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?

asked Jul 20, 2015 at 0:37
7
  • 2
    What is the value of host? Is it possible that socket.gethostname() is returning an invalid hostname? Commented Jul 20, 2015 at 0:49
  • stackoverflow.com/questions/15246088/… Commented Jul 20, 2015 at 0: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 = ''? Commented Jul 20, 2015 at 0: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! Commented Jul 20, 2015 at 0: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? Commented Jul 20, 2015 at 0:55

1 Answer 1

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.)
answered Jul 20, 2015 at 16:42
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.