Error: nodename nor servname provided, or not known (python sockets)

From the docs of socket.gethostname:

Return a string containing the hostname of the machine where the Python interpreter is currently executing.

Note: gethostname() doesn’t always return the fully qualified domain name; use getfqdn() for that.

The host IP is not the same as the hostname. You have a couple of options:

  1. You can either manually assign host to 0.0.0.0 or localhost
  2. You can also query socket.gethostbyname:host = socket.gethostbyname(socket.gethostname()) # or socket.getfqdn() if the former doesn't work

Leave a Comment