HTTPServer: "Cannot assign requested address" on localhost vs 127.0.0.1

Kerrick Staley

With the below Python 3 code:

from http.server import HTTPServer, SimpleHTTPRequestHandler
s = HTTPServer(('localhost', 0), SimpleHTTPRequestHandler)

I get the following error:

Traceback (most recent call last):
  File "repro.py", line 2, in <module>
    s = HTTPServer(('localhost', 0), SimpleHTTPRequestHandler)
  File "/usr/lib/python3.6/socketserver.py", line 453, in __init__
    self.server_bind()
  File "/usr/lib/python3.6/http/server.py", line 136, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/usr/lib/python3.6/socketserver.py", line 467, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 99] Cannot assign requested address

I've noticed that 3 things will prevent this from happening:

  1. Disabling WiFi.
  2. Switching to a different WiFi network (from my home router to tethering on my phone).
  3. Changing 'localhost' in the code to '127.0.0.1'.

So my question is, what's going wrong here? Why does this code fail when it does?

P.S. Here is ip a's output when connected to my home router, and here's its output when tethering.

Kerrick Staley

I believe the issue was that the DNS service on my home WiFi network was not returning the correct information for localhost. I ended up working around this issue by hardcoding the address as 127.0.0.1.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot connect to redis://localhost:6379/0: Error 99 connecting to localhost:6379. Cannot assign requested address

Failed to listen on localhost:8000 (reason: Cannot assign requested address)

RedisInsight on Docker and Redis on Docker: Could not connect: Error 99 connecting to localhost:6379. Cannot assign requested address

Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'

redis.exceptions.ConnectionError: Error 99 connecting to localhost:6379. Cannot assign requested address

Rails, Webpacker, and Docker : Cannot assign requested address - connect(2) for [::1]:3035

Flask, cannot assign requested address

Docker Cannot assign requested address

Nginx on Kubernetes (99: Cannot assign requested address)

Cannot assign requested address (when using Docker)

How to fix 'Cannot assign requested address?'

gin-gonic cannot assign requested address

Bind error (99): Cannot assign requested address

Netty SocketIO - Cannot assign requested address in AWS

requests, cannot assign requested address, out of ports?

OSError: [Errno 99] Cannot assign requested address

Docker for Windows: cannot assign requested address

Errno 99 - Cannot assign requested address

bind: cannot assign requested address on UDPclient in local network

Server binding results in error "Cannot assign requested address"?

"Cannot assign requested address" when trying to set TUN interface netmask

Cannot Assign requested Address in AWS - JAVA Server Socket

MongoDB Replica Set setup is giving - SocketException: Cannot assign requested address

Mesos bind error : Cannot assign requested address [99]

unable to bind socket: Cannot assign requested address issue on ubuntu

redigo: getting dial tcp: connect: cannot assign requested address

Docker and identity server 4, api, angular : Cannot assign requested address

Socket server failed to bind to kafka:29092: Cannot assign requested address

Cannot assign requested address using ServerSocket.socketBind