2013-04-16 31 views
0

我想檢查幾個網址,看他們是否回來,然後再進一步處理它們,我有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'」

感謝

錯誤
+0

你有哪些錯誤? – ThiefMaster

+0

聽起來像一個URL包含一個無效的主機名。 – Aya

+0

它的確如此,它就像www.blahlsghsh.com但肯定應該去嘗試它然後回來,因爲404沒有找到?編輯:jsut掏出假主機名,並再次嘗試,與google.com和bing.com相同的錯誤,所以它不會影響它 – jerrythebum

回答

1

如果self.myList包含的URL列表,你不能直接在HTTPConnection構造像你這樣在這裏使用他們...

for line in self.myList: 
    conn = httplib.HTTPConnection(line) 
    conn.request("HEAD", "/") 

HTTPConnection構造函數應該只傳遞URL的主機名部分,並且請求方法應該被賦予路徑部分。你需要分析的東西,如網址...

import urlparse 

for line in self.myList: 
    url = urlparse.urlparse(line) 
    conn = httplib.HTTPConnection(url.hostname) 
    conn.request("HEAD", url.path) 

更新

您可以更改代碼...

for line in self.myList: 
    try: 
     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)) 
    except: 
     print 'Failed on %r' % line 
     raise 

...和包括全力輸出跑步呢?

更新#2

我不太知道什麼self.fileTxtself.urlFld應該做的,但如果你只是從self.filePath念臺詞,你只需要...

if dlg.ShowModal() == wx.ID_OK: 
    directory, filename = dlg.GetDirectory(), dlg.GetFilename() 
    self.filePath = '/'.join((directory, filename)) 
    self.myList = [line.strip() for line in open(self.filePath, 'r').readlines()] 
+0

好的謝謝。不知道我不能將URL傳遞給httplib。我試圖如你所說 '用於線在self.myList: URL = urlparse.urlparse(線) 康恩= httplib.HTTPConnection(url.hostname) conn.request( 「頭」,url.path) R1 = conn.getresponse() R1 = r1.status,r1.reason self.urlFld.AppendText(url.hostname + 「\噸\ t」 的+ STR(R1))' ,並得到一個cmd錯誤 '回溯(最近呼叫的最後一個): AttributeError:'NoneType'對象沒有屬性'rfind'' – jerrythebum

+0

@directpixel是否可以更新原始問題並附加完整的新代碼和新回溯? – Aya

+0

已經完成了,感謝您的幫助到目前爲止 – jerrythebum