0

Hello I have the following code:

import os, sys
from http.server import HTTPServer, CGIHTTPRequestHandler

webdir = '.'
port = 80
if len(sys.argv) > 1: webdir = sys.argv[1]
if len(sys.argv) > 2: port = int(sys.argv[2])
print("webdir '%s', port %s" % (webdir, port))

os.chdir(webdir)
svraddr = (" ", port)
srvrobj = HTTPServer(svraddr, CGIHTTPRequestHandler)
srvrobj.serve_forever()

However, if I run this code with Administrator privileges, it returns an error:

Traceback (most recent call last):
  File "C:\Users\Nitro\Desktop\web server\webserver.py", line 12, in <module>
    srvrobj = HTTPServer(svraddr, CGIHTTPRequestHandler)
  File "C:\Python33\lib\socketserver.py", line 430, in __init__
    self.server_bind()
  File "C:\Python33\lib\http\server.py", line 135, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "C:\Python33\lib\socketserver.py", line 441, in server_bind
    self.socket.bind(self.server_address)
socket.gaierror: [Errno 11004] getaddrinfo failed

What's wrong?

1
  • Socket Error (Errno?) 11004 on your Windows computer is caused by a corrupt file on your Internet settings. Commented Jul 5, 2014 at 7:21

2 Answers 2

1

For me, changing this line:

svraddr = (" ", port)

to:

svraddr = ("", port)

will solve your problem. The string here (" ") represents what interface the socket should "bind" to: it should be the IP address matching an interface on your machine, but if it isn't, it seems Python will try to look it up (resolve it). " " doesn't resolve. '' means "all interfaces":

For IPv4 addresses, two special forms are accepted instead of a host address: the empty string represents INADDR_ANY

INADDR_ANY is 0.0.0.0, so a more explicit way of saying this is:

svraddr = ('0.0.0.0', port)

"0.0.0.0" means "all interfaces". Your webserver listens on the interfaces (roughly, network cards) that your socket is bound to, in this case, all of them. Often, it's useful to only bind to a particular interface (if you have more than one); also there's the loopback interface, which makes it such that only your machine can connect to the webserver:

svraddr = ('127.0.0.1', port)

(or, alternatively, using the name lookup abilities that were tripping us up earlier)

svraddr = ('localhost', port)
Sign up to request clarification or add additional context in comments.

1 Comment

I tried that, in administrator mode, but it raises an OSError: [Winerror 10013] An attempt was made to access a socket in a way forbidden by its access permissions.
1

Try changing:

svraddr = (" ", port)

To:

svraddr = ("", port)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.