Unable to open unused port on windows

3

Recently I've been unable to open certain unused local ports on my pc(windows10). For example, when I try to open port 1883 with python socket, it gives me this:

[WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions

However, netstat -nq | findstr 1883 in powershell shows nothing, which should indicate the port is not being bound.

Later I found out I have this problem with other ports too. Here's a simple script I used to check:

import socket 
with open('out.txt', 'w') as f:
    for i in range(1,10000):
        s = socket.socket()
        host = socket.gethostname()
        port = i
        try:
            s.bind((host, port))
            print("succeeded",port, file=f)
        except OSError :
            print("failed",port, file=f)

The result looks like this:

...
...
succeeded 1131
succeeded 1132
failed 1133
failed 1134
failed 1135
...
...
failed 1631
failed 1632
succeeded 1633
succeeded 1634
succeeded 1635
...
...
succeeded 1731
succeeded 1732
succeeded 1733
failed 1734
failed 1735
...
...
failed 1832
failed 1833
succeeded 1834
succeeded 1835
...
...

There are several long intervals of ports that cannot open. The length of the failed intervals are either 500 or 100.I sampled some of them and none of them showed up in netstat. Ports beyond a certain point(in this case 2443) are fine. No process is found near the boundaries. These port numbers stay the same after reboot.

Here's what I've tried:

  1. reboot
  2. disable firewall
  3. run as administrator
  4. switch between wifi and lan
  5. reset network with netsh winsock reset
  6. reset local route table with route -f
  7. disable Internet Connection Sharing (ICS) service as suggested in this question

None of the above worked.

aaaaaaaaaaaaa

Posted 2019-06-07T15:49:06.517

Reputation: 31

Question was closed 2019-06-07T20:52:29.060

Try unlocking the administrator account, then logging in as administrator then trying the same in that profile – JohnnyVegas – 2019-06-07T16:49:11.230

@JohnnyVegas Already tried that. – aaaaaaaaaaaaa – 2019-06-07T16:53:24.170

netstat /abo >>c:\output.txt - See what process may be hogging port? – JohnnyVegas – 2019-06-08T06:18:58.570

1I got the exact same problem and I'm still looking for a solution. @aaaaaaaaaaaaa really described the problem well. Sometimes, I don't understand the community's rules on closing. This question is not unclear. The sole fact that nobody knows a solution does not make a question bad. – Mitja – 2019-10-07T07:48:55.363

No answers