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

Posted on

Question :

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

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:Python26libSocketServer.py”,
line 400, in init
self.server_bind() File “C:Python26libSocketServer.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.

Asked By: bitcycle

||

Answer #1:

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.

Answered By: lunixbochs

Answer #2:

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 not been used.

Answered By: David

Answer #3:

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
Answered By: kuz8

Answer #4:

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

Answered By: frmdstryr

Answer #5:

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.)

Answered By: Hardipinder Singh

Answer #6:

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)
Answered By: A.HEDDAR

Answer #7:

I had to allow ..python27python.exe in windows firewall. I don’t need to do this on WinXP or Win8.

Answered By: Brent Smith

Answer #8:

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.

Answered By: Pythonista

Leave a Reply

Your email address will not be published. Required fields are marked *