我想檢查幾個網址,看他們是否回來,然後再進一步處理它們,我有self.myList中的URL列表,然後通過httplib HTTP連接運行這些URL以獲取響應,但是我從cmd中的httplib獲得了一大堆錯誤。httplib python/wxpython。襪子流錯誤
代碼工作,因爲我已經與下面的測試,它正確地回來,並設置在wx.TextCtrl值:
#for line in self.myList:
conn = httplib.HTTPConnection("www.google.com")
conn.request("HEAD", "/")
r1 = conn.getresponse()
r1 = r1.status, r1.reason
self.urlFld.SetValue(str(r1))
它只是似乎並不當我通過它的工作來自myList的超過1個網址。
for line in self.myList:
conn = httplib.HTTPConnection(line)
conn.request("HEAD", "/")
r1 = conn.getresponse()
r1 = r1.status, r1.reason
self.urlFld.SetValue(line + "\t\t" + str(r1))
我得到CMD的錯誤是
Traceback (most recent call last):
File "gui_texteditor_men.py", line 96, in checkBtnClick
conn.request("HEAD", "/")
File "C:\Python27\lib\httplib.py", line 958, in request
self._send_request(method, url, body, headers)
File "C:\Python27\lib\httplib.py", line 992, in _send_request
self.endheaders(body)
File "C:\Python27\lib\httplib.py", line 954, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 814, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 776, in send
self.connect()
File "C:\Python27\lib\httplib.py", line 757, in connect
self.timeout, self.source_address)
File "C:\Python27\lib\socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11004] getaddrinfo failed
編輯,使用更新裏urlparse代碼。我已經導入了urlparse。
for line in self.myList:
url = urlparse.urlparse(line)
conn = httplib.HTTPConnection(url.hostname)
conn.request("HEAD", url.path)
r1 = conn.getresponse()
r1 = r1.status, r1.reason
self.urlFld.AppendText(url.hostname + "\t\t" + str(r1))
與回溯,
C:\Python27\Coding>python gui_texteditor_men.py
Traceback (most recent call last):
File "gui_texteditor_men.py", line 97, in checkBtnClick
conn = httplib.HTTPConnection(url.hostname)
File "C:\Python27\lib\httplib.py", line 693, in __init__
self._set_hostport(host, port)
File "C:\Python27\lib\httplib.py", line 712, in _set_hostport
i = host.rfind(':')
AttributeError: 'NoneType' object has no attribute 'rfind'
我現在有www.google.com和www.bing.com在.txt文件,當它拋出這個錯誤。
編輯2 @綾,
看起來失敗,原因是2個網址之間的 「\ n」。我以爲我用.strip()編碼去除「\ n」,但似乎沒有任何效果。
Failed on u'http://www.google.com\nhttp://www.bing.com'
Traceback (most recent call last):
File "gui_texteditor_men.py", line 99, in checkBtnClick
conn.request("HEAD", url.path)
File "C:\Python27\lib\httplib.py", line 958, in request
self._send_request(method, url, body, headers)
File "C:\Python27\lib\httplib.py", line 992, in _send_request
self.endheaders(body)
File "C:\Python27\lib\httplib.py", line 954, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 814, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 776, in send
self.connect()
File "C:\Python27\lib\httplib.py", line 757, in connect
self.timeout, self.source_address)
File "C:\Python27\lib\socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11004] getaddrinfo failed
我又看看我.strip()當我打開文件,
if dlg.ShowModal() == wx.ID_OK:
directory, filename = dlg.GetDirectory(), dlg.GetFilename()
self.filePath = '/'.join((directory, filename))
self.fileTxt.SetValue(self.filePath)
self.urlFld.LoadFile(self.filePath)
self.myList = self.urlFld.GetValue().strip()
,現在回跡與 「失敗的u'h'」
感謝
錯誤
你有哪些錯誤? – ThiefMaster
聽起來像一個URL包含一個無效的主機名。 – Aya
它的確如此,它就像www.blahlsghsh.com但肯定應該去嘗試它然後回來,因爲404沒有找到?編輯:jsut掏出假主機名,並再次嘗試,與google.com和bing.com相同的錯誤,所以它不會影響它 – jerrythebum