gaierror:[Errno -2]姓名或服务未知

时间:2011-03-07 16:00:38

标签: python urllib2 urllib

  def make_req(data, url, method='POST')  
    params = urllib.urlencode(data)
    headers = {"Content-type": "application/x-www-form-urlencoded",
               "Accept": "text/plain",
               }
    conn = httplib.HTTPSConnection(url)
    conn.request(method, url, params, headers)
    response = conn.getresponse()
    response_data = response.read()
    conn.close()

但它正在抛出:in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): gaierror: [Errno -2] Name or service not known

是什么原因?这个错误是什么?

1 个答案:

答案 0 :(得分:7)

您需要使用相对于服务器的URI调用request()。如果urlwww.google.com/images?q=test则必须执行:

conn = httplib.HTTPSConnection('www.google.com')
conn.request('GET', '/images?q=test')