0

I am following this example,

#!/usr/bin/python           # This is server.py file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
while True:
   c, addr = s.accept()     # Establish connection with client.
   print 'Got connection from', addr
   c.send('Thank you for connecting')
   c.close()                # Close the connection

and I am getting this error despite good network:

 >>> s.bind((host, port)) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Applications/anaconda/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

How can I fix this?

Pippi
  • 2,451
  • 8
  • 39
  • 59
  • 2
    What is the value of `host`? Is it possible that `socket.gethostname()` is returning an invalid hostname? – David Zemens Jul 20 '15 at 00:49
  • http://stackoverflow.com/questions/15246088/what-does-this-socket-gaierror-mean – David Zemens Jul 20 '15 at 00:50
  • 1
    What happens if you try: `s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)`? And if that doesn't work try change `host = ''`? – shuttle87 Jul 20 '15 at 00:50
  • @shuttle87 that *should* do it. I was trying to find my socket code but the `.AF_INET` is what we've used before -- I think that will work! – David Zemens Jul 20 '15 at 00:51
  • host is valid, return machine's name. I did s = socket.socket(socket.AF_INET, socket.SOCK_STREAM), s is a valid socket object, but get the same error. host = '' works but why does it work since according to python doc, it should be a string of host name or an ip address if using AF_INET? – Pippi Jul 20 '15 at 00:55
  • With your current code what exactly do you get if you do `print(host)`? – shuttle87 Jul 20 '15 at 01:06
  • hmmmm I did get that error, once, using your code, but now not able to reproduce the error. – David Zemens Jul 20 '15 at 01:10

1 Answers1

3

Let's take a look at the docs:

socket.gethostname()

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

If you want to know the current machine’s IP address, you may want to use gethostbyname(gethostname()). This operation assumes that there is a valid address-to-host mapping for the host, and the assumption does not always hold.

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

I guess this is what's happening: bind is trying to establish IP address for the host, but it fails. Run host = socket.gethostbyname(socket.gethostname()) and instead of a valid IP address you'll most probably see the same error as when calling bind.

You say the returned hostname is valid, but you have to make sure it's recognised by the DNS responder. Does the resolution work when doing, for example, ping {hostname} from the command line?

Possible solutions would be:

  1. Fix your local DNS resolution.
  2. Use host = socket.getfqdn() (in case you were not getting the fully qualified name which then couldn't be resolved properly). Even if it works I think you should try and fix the local resolution.
  3. Use empty host (host = ''), which on bind would mean "listen on all available interfaces". (This is the first example in the docs.)
tomasz
  • 12,574
  • 4
  • 43
  • 54