socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
Asked Answered
S

24

69

I'm trying to create a custom TCP stack using Python 2.6.5 on Windows 7 to serve valid http page requests on port 80 locally. But, I've run into a snag with what seems like Windows 7 tightened up security. This code worked on Vista.

Here's my sample code:

import SocketServer
import struct

class MyTCPHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        headerText = """HTTP/1.0 200 OK
                        Date: Fri, 31 Dec 1999 23:59:59 GMT
                        Content-Type: text/html
                        Content-Length: 1354"""
        bodyText = "<html><body>some page</body></html>"
        self.request.send(headerText + "\n" + bodyText)

if __name__ == "__main__":
    HOST, PORT = "localhost", 80
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
    server.serve_forever()

C:\python>python TestServer.py Traceback (most recent call last):
File "TestServer.py", line 19, in server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) File "C:\Python26\lib\SocketServer.py", line 400, in init self.server_bind() File "C:\Python26\lib\SocketServer.py", line 411, in server_bind self.socket.bind(self.server_address) File "", line 1, in bind

socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions

How exactly do I get this to work on Windows 7?

[Edit on 5/5/2010 @ 2344 PDT] This answer explains that the error is caused by the need for elevated / superuser privileges when accessing ports lower than 1024. I'm going to try using a higher port number to see if that works. However, I still would like to know why my local admin account can't access port 80.

Scotney answered 6/5, 2010 at 6:12 Comment(3)
Your local admin account may not be able to bind to port 80 because there is already another process (server) bound to it. telnet 127.0.0.1 80 will quickly show you if port 80 is already bound.Trembles
@Trembles That should give a different error.Mohr
@msw/everybody note that a telnet client isn't installed on Win7 by default.Fireguard
S
29

On Windows Vista/7, with UAC, administrator accounts run programs in unprivileged mode by default.

Programs must prompt for administrator access before they run as administrator, with the ever-so-familiar UAC dialog. Since Python scripts aren't directly executable, there's no "Run as Administrator" context menu option.

It's possible to use ctypes.windll.shell32.IsUserAnAdmin() to detect whether the script has admin access, and ShellExecuteEx with the 'runas' verb on python.exe, with sys.argv[0] as a parameter to prompt the UAC dialog if needed.

Shovelhead answered 6/5, 2010 at 7:40 Comment(1)
make sure you import ctypes before running ctypes.windll.shell32.IsUserAnAdmin()Reproach
C
62

I just encountered the same issue, my system is Win7. Just use the command on terminal like: netstat -na|findstr port, you will see the port has been used. So if you want to start the server without this message, you can change other port that is not in use.

Compensatory answered 18/2, 2013 at 2:11 Comment(4)
This was the case for me on Win8Defection
i was also able to resolve this error by changing the application port in Win8Khalif
Or stop/change the process that is using the other port. Many ways to find which process: #48698Horrendous
added python.exe and can now run my code in windows powershell. thanks!Kaliningrad
W
34

Try to run the server at a different port. Worked for me:

python manage.py runserver 127.0.0.1:7000

Explanation:

as mentioned on Django documentation:

If you run this script as a user with normal privileges (recommended), you might not have access to start a port on a low port number. Low port numbers are reserved for the superuser (root).

This server uses the WSGI application object specified by the WSGI_APPLICATION setting.

DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. (And that’s how it’s gonna stay. We’re in the business of making Web frameworks, not Web servers, so improving this server to be able to handle a production environment is outside the scope of Django.)

Weeper answered 4/3, 2019 at 8:31 Comment(1)
@Armali I hope this provides a plausible explanationWeeper
S
29

On Windows Vista/7, with UAC, administrator accounts run programs in unprivileged mode by default.

Programs must prompt for administrator access before they run as administrator, with the ever-so-familiar UAC dialog. Since Python scripts aren't directly executable, there's no "Run as Administrator" context menu option.

It's possible to use ctypes.windll.shell32.IsUserAnAdmin() to detect whether the script has admin access, and ShellExecuteEx with the 'runas' verb on python.exe, with sys.argv[0] as a parameter to prompt the UAC dialog if needed.

Shovelhead answered 6/5, 2010 at 7:40 Comment(1)
make sure you import ctypes before running ctypes.windll.shell32.IsUserAnAdmin()Reproach
S
19

For me it was complaining like that on Windows 7 x64 when I had another process already listening on that same port.

It is possible to see currently occupied (bound) ports by running

netstat -ban
Stacey answered 19/9, 2015 at 23:42 Comment(0)
T
12

McAfee was blocking it for me. I had to allow the program in the access protection rules

  1. Open VirusScan
  2. Right click on Access Protection and choose Properties
  3. Click on "Anti-virus Standard Protection"
  4. Select rule "Prevent mass mailing worms from sending mail" and click edit
  5. Add the application to the Processes to exclude list and click OK

See http://www.symantec.com/connect/articles/we-are-unable-send-your-email-caused-mcafee

Tolly answered 15/8, 2014 at 17:58 Comment(1)
Thanks man saved my life i was stuck for past 2 days done lot of changes but worked from this solutionTymothy
C
10

Your local port is using by another app. I faced the same problem! You can try the following step:

  1. Go to command line and run it as administrator!

  2. Type:

    netstat -ano | find ":5000"
    => TCP    0.0.0.0:5000           0.0.0.0:0              LISTENING       4032
       TCP    [::]:5000              [::]:0                 LISTENING       4032
    
  3. Type:

    TASKKILL /F /PID 4032
    

    => SUCCESS: The process with PID 4032 has been terminated.

Note: My 5000 local port was listing by PID 4032. You should give yours!

Crowbar answered 16/3, 2020 at 4:40 Comment(1)
might be a good idea to see what app is using the port before blindly killing the process id. For that see pslist from SysInternals (and pskill).Merchandise
F
8

socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions

Got this with flask :

Means that the port you're trying to bind to, is already in used by another service or process : got a hint on this in my code developed on Eclipse / windows :

if __name__ == "__main__":
     # Check the System Type before to decide to bind
     # If the system is a Linux machine -:) 
     if platform.system() == "Linux":
        app.run(host='0.0.0.0',port=5000, debug=True)
     # If the system is a windows /!\ Change  /!\ the   /!\ Port
     elif platform.system() == "Windows":
        app.run(host='0.0.0.0',port=50000, debug=True)
Flagitious answered 31/7, 2017 at 10:11 Comment(1)
Have a look here: answers.microsoft.com/en-us/windows/forum/windows_7-networking/…Factfinding
M
5

Here is a simple solution to your problem..this error occurs either due to another process or bunch of processes running on the port which you are trying to run you app on. Open cmd as an admin This may also occure if you have setup your server as a SERVICE and the service breaks. Give below command

netstat -ano | findstr portNo

you'll be able to see list of active task on that port. you can close all unnecessary task by using

taskkill /pid taskNo /F

once done restart your server.

Menadione answered 21/1, 2022 at 9:27 Comment(0)
A
4

Just run on ports above 1024 , anything below is privileged, its the same deal with Linux, i use 5000 for example on wins without any UAC priv escalation.

Anglesey answered 25/9, 2018 at 20:22 Comment(0)
G
4

I ran into this issue today, but the webserver's port was not already used. Running the following commands in elevated terminal solved the problem.

net stop winnat
net start winnat

I think it had something to do with local sql server running on docker, and the webserver not being able to access it.

Glyph answered 5/8, 2023 at 7:58 Comment(0)
R
3

It Seems the Port 80 is already in use. Try to Use some other Port which is not in use by any other application in your System.

Runyon answered 19/1, 2019 at 18:16 Comment(0)
S
3

I solved this on Windows 10 by editing an outbound firewall rule. Right click "allow" on rule "Block network access for R local user accounts in SQL Server instance MSSQLSERVER"

from Windows 10 Firewall - Outbound rules- this is what was blocking my instance

Screenshot from Windows 10 Firewall - Outbound rules- this is what was blocking my instance

Sennet answered 7/3, 2020 at 5:22 Comment(0)
S
2

I had to allow ..\python27\python.exe in windows firewall. I don't need to do this on WinXP or Win8.

Sneaky answered 12/10, 2013 at 23:2 Comment(0)
C
2

Try using a different port than 80

Comprehension answered 20/12, 2021 at 15:10 Comment(0)
T
1

The main problem is port number used by another application.So you can change the port number to unused one as shown below.

In windows you can view the used port numbers used by different apps in windows task manager.

python manage.py runserver 127.0.0.1:portnumber
Ex: python manage.py runserver 127.0.0.1:8080
Toowoomba answered 25/4, 2019 at 5:0 Comment(1)
"In windows you can view the used port numbers used by different apps in windows task manager." I can't see them. Please double check this and provide instructions.Fraze
S
1

Disable Access Protection in Antivirus,

I faced same issue at last found the below logs from antivirus.

Blocked by Access Protection rule NT AUTHORITY\SYSTEM C:\WINDOWS\SYSTEM32\SVCHOST.EXE C:\PROGRAM FILES (X86)\MCAFEE\VIRUSSCAN ENTERPRISE\MCCONSOL.EXE Common Standard Protection:Prevent termination of McAfee processes Action blocked : Terminate Blocked by port blocking rule C:\USERS\username\APPDATA\LOCAL\PROGRAMS\PYTHON\PYTHON37-32\PYTHON.EXE Anti-virus Standard Protection:Prevent mass mailing worms from sending mail

Sleepless answered 12/8, 2019 at 18:17 Comment(0)
M
1

I just found in my case Kaspersky Internet Security 2019 Firewall was blocking net access for python. Disabling firewall working smoothly. Or adding a exception rules for python app and all file extension with *.py will also work.

Manuel answered 15/5, 2020 at 14:38 Comment(1)
could you please show more specific info, and show me how to add more rule? Because I checked, the python is truthed by Kaspersky.Photochromy
O
1

In Windows check "World Wide Web Publishing Service" if it is running, because when you install IIS it has a service that auto. starts and listens 80, 443 port.

Opine answered 28/12, 2021 at 8:52 Comment(0)
G
0
netstat -ano | find ":80"

then TASKKILL /F /PID 4032

as rafee noor said, but this doesn't work when Port is being used by SYSTEM

Port 80 is being used by SYSTEM (PID 4), what is that?

or

NET stop HTTP

this works in above case

Gaskell answered 13/1, 2022 at 4:35 Comment(0)
S
0

For those who have encountered this error while using vscode for remote host:

  1. Open Terminal ( View -> Terminal ) or ( CTRL+ ` )
  2. Click on tab PORTS
  3. Delete the port that is allocated by remote host as shown in picture.

enter image description here

Now you can use the port.

Semolina answered 27/5, 2023 at 12:44 Comment(0)
M
0

this worked. windows 11. assign the port=3000 or any, manually in app.run()

if __name__ == '__main__':
    app.run( port=8080, debug=True)

then a window appeared ,clicked allowed
i think flask server and web app both try to run on
a single port in my case 5000 ,this may be the reason
,so changing port worked. i am not sure its true .

Mytilene answered 23/11, 2023 at 12:24 Comment(0)
D
0

After spending hours debugging in Docker network and adjusting Windows ports and settings, I found that disconnecting from the VPN solved the issue.

Dismay answered 27/11, 2023 at 13:50 Comment(0)
T
0

it might help you: check if you opened wireguard VPN software before, such as tailscale. It seems that if u close such software while you close ur socket in your program, I witeness some kind of race condition result in errno 10013 I guess itz windows' bug.

Thermion answered 1/7, 2024 at 2:21 Comment(0)
M
-3

I found a solution to solve this problem in Python.

go to c:\python27\ directory and rigtlcick python.exe and tab to compaitbility and select the admin privilege option and apply the changes. Now you issue the command it allows to create the socket connection.

Menzies answered 9/11, 2014 at 15:18 Comment(1)
This is insecure as it runs all Python scripts with administrator privileges. Moreover, the question has had an accepted answer for several years.Stokowski

© 2022 - 2025 — McMap. All rights reserved.