socket.gaierror: [Errno -2] Name or service not known

匿名 (未验证) 提交于 2019-12-03 08:28:06

问题:

Guys i'm a newbie to the socket programming Following program is a client program which request a file from the server,But i'm getting the error as show below.. My input is GET index.html and the code is Can anyone solve this error...?

#!/usr/bin/env python  import httplib import sys   http_server = sys.argv[0]  conn = httplib.HTTPConnection(http_server)  while 1: cmd = raw_input('input command (ex. GET index.html): ') cmd = cmd.split()  if cmd[0] == 'exit':      break   conn.request(cmd[0],cmd[1])   rsp = conn.getresponse()   print(rsp.status, rsp.reason) data_received = rsp.read() print(data_received)  conn.close()      input command (ex. GET index.html): GET index.html Traceback (most recent call last): File "./client1.py", line 19, in <module> conn.request(cmd[0],cmd[1]) File "/usr/lib/python2.6/httplib.py", line 910, in request self._send_request(method, url, body, headers) File "/usr/lib/python2.6/httplib.py", line 947, in _send_request self.endheaders() File "/usr/lib/python2.6/httplib.py", line 904, in endheaders self._send_output() File "/usr/lib/python2.6/httplib.py", line 776, in _send_output self.send(msg) File "/usr/lib/python2.6/httplib.py", line 735, in send   self.connect()  File "/usr/lib/python2.6/httplib.py", line 716, in connect   self.timeout) File "/usr/lib/python2.6/socket.py", line 500, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): socket.gaierror: [Errno -2] Name or service not known 

回答1:

sys.argv[0] is not what you think it is. sys.argv[0] is the name of the program or script. The script's first argument is sys.argv[1].



回答2:

The problem is that the first item in sys.argv is the script name. So your script is actually using your filename as the hostname. Change the 5th line to:

http_server = sys.argv[1] 

More info here.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!