2013-03-02 25 views
1

我有一個腳本,它可以獲得Internet上很多頁面的HTTP Header,並且使用Python中的httplibHttplib無法處理特定域名

我的問題是在specific domain(可能是其他人),httplib引發異常,我不明白爲什麼。

>>> import httplib 
>>> http = httplib.HTTPConnection('iswtc.la') 
>>> http.request('GET', '/a') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib64/python2.6/httplib.py", line 914, in request 
    self._send_request(method, url, body, headers) 
    File "/usr/lib64/python2.6/httplib.py", line 951, in _send_request 
    self.endheaders() 
    File "/usr/lib64/python2.6/httplib.py", line 908, in endheaders 
    self._send_output() 
    File "/usr/lib64/python2.6/httplib.py", line 780, in _send_output 
    self.send(msg) 
    File "/usr/lib64/python2.6/httplib.py", line 739, in send 
    self.connect() 
    File "/usr/lib64/python2.6/httplib.py", line 720, in connect 
    self.timeout) 
    File "/usr/lib64/python2.6/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 

什麼是這個特定領域不同,我該怎麼處理呢?

PS:這不是我的代碼,因爲這工作得很好:

>>> http = httplib.HTTPConnection('bit.ly') 
>>> http.request('GET', '/a') 
+0

好吧,如果工作正常這是你的代碼,因爲它不起作用... – 2013-03-02 20:35:39

+0

@IanCarroll你是什麼意思?我把這段代碼放在我的'python'控制檯中,我沒有問題。 – Arthur 2013-03-02 20:38:13

回答

1

bit.ly存在,而iswtc.la並不:

$ nslookup bit.ly 

Non-authoritative answer: 
Name: bit.ly 
Address: 69.58.188.39 
Name: bit.ly 
Address: 69.58.188.40 


$ nslookup iswtc.la 

** server can't find iswtc.la: NXDOMAIN 
+0

對。我現在感到啞巴。我非常專注於我的代碼,忘記檢查域是否存在...讓我們來處理這個異常。 – Arthur 2013-03-02 20:42:24