socket.gaierror上的httplib错误:

时间:2016-12-06 10:08:31

标签: python httplib

我想获取具有以下代码的网站的Url状态。对于一个网站(webscraper.io),我收到了一个错误。我的剧本是:

import httplib
url = "http://webscraper.io/"
if 'http' in url:
     url = url.replace('http://', '').strip()
conn = httplib.HTTPConnection(url)
conn.request("GET",'')
r1 = conn.getresponse()
print 'r1.Status code=', r1.status 

我收到以下错误:

Traceback (most recent call last):
File "TestSatusline.py", line 23, in <module>
     conn.request("GET",'')
File "/usr/lib/python2.7/httplib.py", line 1017, in request
    self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py", line 1051, in _send_request
    self.endheaders(body)
File "/usr/lib/python2.7/httplib.py", line 1013, in endheaders
    self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 864, in _send_output
    self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 826, in send
     self.connect()
File "/usr/lib/python2.7/httplib.py", line 807, in connect
     self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
     for res in getaddrinfo(host, port, 0, SOCK_STREAM):
 socket.gaierror: [Errno -2] Name or service not known

有人有任何想法吗?

感谢

1 个答案:

答案 0 :(得分:0)

之后

if 'http' in url:
     url = url.replace('http://', '').strip()

在您的代码中,网址为webscraper.io/,应为webscraper.io

使用urlparse

import httplib
import urlparse

url = "http://webscraper.io/"
o = urlparse.urlparse(url)
conn = httplib.HTTPConnection(o.netloc)
conn.request("GET",'')
r1 = conn.getresponse()
print 'r1.Status code=', r1.status

输出

r1.Status code= 200
你可以看看请求。 http://docs.python-requests.org/en/master/