[英]httplib python/wxpython. sock stream error
我正在尝试检查一些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'
现在,当我引发此错误时,我在.txt文件中有www.google.com和www.bing.com。
编辑2 @ Aya,
似乎由于两个URL之间的“ \\ n”而失败。 我以为我用.strip()删除了它的代码,但是似乎没有任何效果。
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()
现在,它的回溯错误为“ Failed on u'h'”
谢谢
如果self.myList
包含URL列表,则不能像在HTTPConnection
那样直接在HTTPConnection
构造函数中使用它们。
for line in self.myList:
conn = httplib.HTTPConnection(line)
conn.request("HEAD", "/")
仅应将URL的主机名部分传递给HTTPConnection
构造函数,并应将路径部分赋予request方法。 您需要使用以下内容解析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()]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.