当urllib2.request到达超时,一个urllib2.URLError异常.重试建立连接的pythonic方法是什么?
我有读取这样的网址的代码:
from urllib2 import Request, urlopen
req = Request(url)
for key, val in headers.items():
req.add_header(key, val)
res = urlopen(req, timeout = timeout)
# This line blocks
content = res.read()
Run Code Online (Sandbox Code Playgroud)
超时适用于urlopen()调用.但是然后代码进入res.read()调用,我想要读取响应数据,并且不会在那里应用超时.因此,读取调用可能几乎永远挂起,等待来自服务器的数据.我发现的唯一解决方案是使用一个信号来中断read(),因为我正在使用线程,所以不适合我.
还有哪些其他选择?是否有用于处理读取超时的Python的HTTP库?我看过httplib2和请求,他们似乎遇到了与上面相同的问题.我不想使用套接字模块编写自己的非阻塞网络代码,因为我认为应该已经有了一个库.
更新:以下解决方案都没有为我做.您可以自己查看设置套接字或urlopen超时在下载大文件时无效:
from urllib2 import urlopen
url = 'http://iso.linuxquestions.org/download/388/7163/http/se.releases.ubuntu.com/ubuntu-12.04.3-desktop-i386.iso'
c = urlopen(url)
c.read()
Run Code Online (Sandbox Code Playgroud)
至少在使用Python 2.7.3的Windows上,超时被完全忽略.
首先,我的问题与此问题非常相似.我希望urllib.urlopen()超时以生成我可以处理的异常.
这不属于URLError吗?
try:
response = urllib.request.urlopen(url, timeout=10).read().decode('utf-8')
except (HTTPError, URLError) as error:
logging.error(
'Data of %s not retrieved because %s\nURL: %s', name, error, url)
else:
logging.info('Access successful.')
Run Code Online (Sandbox Code Playgroud)
错误消息:
resp = urllib.request.urlopen(req,timeout = 10).read().decode('utf-8')
File"/usr/lib/python3.2/urllib/request.py",第138行,在urlopen中
return opener.open(url,data,timeout)
文件"/usr/lib/python3.2/urllib/request.py",第369行,打开
响应= self._open(req,data)
文件"/ usr/lib /python3.2/urllib/request.py",第387 行,在_open'_open
',req中)
文件"/usr/lib/python3.2/urllib/request.py",第347行,在_call_chain
result = func(*args)
文件"/usr/lib/python3.2/urllib/request.py",第1156行,在http_open中
返回self.do_open(http.client.HTTPConnection,req)
文件"/usr/lib/python3.2/ urllib/request.py",第1141行,在
do_open 中r = h.getresponse()
文件"/usr/lib/python3.2/http/client.py",第1046行,在getresponse
response.begin()
文件中"/ usr/lib/python3.2/http/client.py",第346行,在开始
版本,状态,原因= self._read_status()
文件"/usr/lib/python3.2/http/client.py",行308,在_read_status
line = str(self.fp.readline(_MAXLINE + 1),"iso-8859-1")
文件"/usr/lib/python3.2/socket.py ",第276行,在readinto中
返回self._sock.recv_into(b)socket.timeout
:超时
当Python 3重新组织urllib和urllib2模块时,Python …
我的问题很简单.我有一个try/except代码.在尝试中我有一些http请求尝试,除了我有几种方法来处理我得到的异常.
现在我想在我的代码中添加一个时间参数.这意味着尝试只会持续'n'秒.否则接受它除外.
在自由语言中,它将显示为:
try for n seconds:
doSomthing()
except (after n seconds):
handleException()
Run Code Online (Sandbox Code Playgroud)
这是中码.不是功能.我必须抓住超时并处理它.我不能只是继续代码.
while (recoveryTimes > 0):
try (for 10 seconds):
urllib2.urlopen(req)
response = urllib2.urlopen(req)
the_page = response.read()
recoveryTimes = 0
except (urllib2.URLError, httplib.BadStatusLine) as e:
print str(e.__unicode__())
print sys.exc_info()[0]
recoveryTimes -= 1
if (recoveryTimes > 0):
print "Retrying request. Requests left %s" %recoveryTimes
continue
else:
print "Giving up request, changing proxy."
setUrllib2Proxy()
break
except (timedout, 10 seconds has passed)
setUrllib2Proxy()
break
Run Code Online (Sandbox Code Playgroud)
我需要的解决方案是try (for 10 seconds) …
我有一个URL列表
我使用以下内容检索其内容:
for url in url_list:
req = urllib2.Request(url)
resp = urllib2.urlopen(req, timeout=5)
resp_page = resp.read()
print resp_page
Run Code Online (Sandbox Code Playgroud)
超时时,程序崩溃.我只想阅读下一个URL,如果有的话socket.timeout: timed out.这该怎么做?
谢谢
我正在努力尝试,除了 python 3
我可以捕获 HTTPError(Bad Request) 和 URLError(no host/route name found)
但是我还没有捕捉到超时错误。
while True:
try:
f = urllib.request.urlretrieve(url,csvname)
except urllib.error.HTTPError as e: #
print(str(chl) + " Error:" + str(e))
continue
except urllib.error.URLError as e:
continue
except socket.timeout as e:
#I can't catch time out error here
continue
Run Code Online (Sandbox Code Playgroud)
它返回这个。
如何防止脚本因超时错误而停止?
Traceback (most recent call last):
File "wisdom2.py", line 84, in <module>
f = urllib.request.urlretrieve(url,csvname)
File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 186, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 161, in urlopen …Run Code Online (Sandbox Code Playgroud)