我正在尝试检查几个URL以查看它们是否在我进一步操作之前恢复正常,我在self.myList中有一个URL列表,然后通过httplib HTTP Connection运行这些URL以获取响应但是我从cmd中的httplib中得到了大量错误。
代码工作正常,因为我已经使用下面的代码进行了测试,它正确地返回并在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个URL时,它似乎不起作用。
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 @ Aya,
由于2个URL之间的“\ 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()
现在它的回溯错误“失败了”
由于
答案 0 :(得分:1)
如果self.myList
包含网址列表,则不能像在此处一样直接在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.fileTxt
和self.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()]