socket.gaierror:名称或服务未知

伙计们,我是一个新手到套接字编程下面的程序是一个客户端程序,从服务器请求一个文件,但我得到的错误,如下所示..我的input是GET的index.html和代码是谁能解决这个错误…?

#!/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 

Solutions Collecting From Web of "socket.gaierror:名称或服务未知"

sys.argv [0]不是你想象的那样。 sys.argv [0]是程序或脚本的名称。 脚本的第一个参数是sys.argv [1]。

问题是sys.argv中的第一项是脚本名称。 所以你的脚本实际上是使用你的文件名作为主机名。 将第五行更改为:

 http_server = sys.argv[1] 

更多信息在这里。