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


i trying learn how use sockets using tutorial:
https://www.tutorialspoint.com/python/python_networking.htm
have copied code site directory , ran done in tutorial got errors. here code tutorial.

#!/usr/bin/python           # server.py file  import socket               # import socket module  s = socket.socket()         # create socket object host = 'localhost' # local machine name port = 12345                # reserve port service. s.bind((host, port))        # bind port  s.listen(5)                 # wait client connection. while true:     c, addr = s.accept()     # establish connection client.     print("asdf")     c.send('thank connecting')     c.close()                # close connection 

and client.py

#!/usr/bin/python           # client.py file  import socket               # import socket module  s = socket.socket()         # create socket object host = socket.gethostname() # local machine name port = 12345                # reserve port service.  s.connect((host, port)) print s.recv(1024) s.close                     # close socket when done 

these console commands ran:

python server.py & python client.py 

i got errors after running command:

traceback (most recent call last):   file "client.py", line 9, in <module>     s.connect((host, port))   file     "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/soc    ket.py", line 228, in meth return getattr(self._sock,name)(*args) socket.gaierror: [errno 8] nodename nor servname provided, or not known 

in case helpful, version of python using python 2.7.10 , use mac version 10.12.6

thanks in advance

from docs of socket.gethostname:

return string containing hostname of machine python interpreter executing.

note: gethostname() doesn’t return qualified domain name; use getfqdn() that.

the host ip not same hostname. have couple of options:

  1. you can either manually assign host 0.0.0.0 or localhost

  2. you can query socket.gethostbyname:

    host = socket.gethostbyname(socket.gethostname()) # or socket.getfqdn() if former doesn't work 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -