问题描述
我正在使用Python 2.3s超时套接字并且有这样的代码来读取页面
来自网络的
:
请求= ...
self.page = urllib2.urlopen(请求)
及更高版本:
试试:
self.data = self.page.read()
除了socket.error,e:...
除了socket.timeout:...
除了超时:...
但这些例外都没有捕获超时阅读数据。我仍然得到以下异常,我无法处理:
....
文件F:\\ \\CrawlingFramework\Rules\Tools\__init __ PY"。 ,第91行,在__init__
self.data = self.page.read()
文件C:\Python23 \lib \ socket.py ,第283行,阅读
data = self._sock.recv(recv_size)
超时:超时
任何提示关于如何处理这个异常或者出了什么问题?
问候,
Achim
套接字。 timeout是socket.error的子类,因此超时异常
应该被第一个except子句捕获。
但是,我可以重现你未被捕获的异常以下
极简代码:
来自urllib2 import urlopen
进口插座
slowurl =" http://127.0.0.1/timeout?delay = 100"
socket.setdefaulttimeout(1)
data = urlopen(slowurl)
试试:
data.read()
除外:#应该抓住任何异常
print" ;超时提升并抓住了 #这从未显示
所以似乎有*没有*方法来捕获错误。
我认为你应该提交错误报告。
Peter
我/我如何做?
Achim
嗯。
以下代码有什么问题?它似乎做你需要的:
#============================= ==================== ======
来自urllib2 import urlopen
import socket
import sys
slowurl =" http://127.0.0.1/cgi-bin/longWait.py?wait = 10"
socket.setdefaulttimeout(1)
试试:
data = urlopen(slowurl)
data.read()
除了socket.error:
errno,errstr = sys.exc_info()[:2]
if errno == socket.timeout:
打印有超时
否则:
打印还有其他一些套接字错误
#================================================ = =========
问候,
-
alan kennedy
-------------------------------------------- ---------
在这里查看http标题:
emai l alan:
Hi,
I''m using Python 2.3s timeout sockets and have code like this to read a page
from web:
request = ...
self.page = urllib2.urlopen(request)
and later:
try:
self.data = self.page.read()
except socket.error,e: ...
except socket.timeout: ...
except timeout: ...
but none of these excepts catches the the timeout while reading the data. I
still get the following exception, which I cannot handle:
....
File "F:\CrawlingFramework\Rules\Tools\__init__.py" , line 91, in __init__
self.data = self.page.read()
File "C:\Python23\lib\socket.py", line 283, in read
data = self._sock.recv(recv_size)
timeout: timed out
Any hint on how to handle this exception or what''s going wrong?
regards,
Achim
socket.timeout is a subclass of socket.error, so the timeout exception
should be caught by the first except clause.
However, I could reproduce your uncaught exception with the following
minimalist code:
from urllib2 import urlopen
import socket
slowurl = "http://127.0.0.1/timeout?delay=100"
socket.setdefaulttimeout(1)
data = urlopen(slowurl)
try:
data.read()
except: # should catch ANY exception
print "Timeout raised and caught" # this never shows
So it seems there is *no* way to catch the error.
I think you should file a bug report.
Peter
Where/How do I do that?
Achim
Hmmm.
What is wrong with the following code? It seems to do what you need:
#================================================= ======
from urllib2 import urlopen
import socket
import sys
slowurl = "http://127.0.0.1/cgi-bin/longWait.py?wait=10"
socket.setdefaulttimeout(1)
try:
data = urlopen(slowurl)
data.read()
except socket.error:
errno, errstr = sys.exc_info()[:2]
if errno == socket.timeout:
print "There was a timeout"
else:
print "There was some other socket error"
#================================================= =========
regards,
--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
这篇关于如何捕获套接字超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!