How to catch socket timeout?
Peter Otten
__peter__ at web.de
Sun Sep 21 07:17:37 EDT 2003
Achim Domma wrote:
> 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:
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
More information about the Python-list
mailing list