How to catch socket timeout?
Bob Halley
halley at play-bow.org
Sun Sep 21 17:04:41 EDT 2003
"Achim Domma" <domma at procoders.net> writes:
> 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: ...
As another poster pointed out, socket.timeout is a subclass of
socket.error. (This was so you could write code that treated all
socket errors alike if you wanted timeouts but didn't need to deal
with them separately.)
Section 7.4 of the Language Reference says:
[...] When an exception occurs in the try suite, a search for
an exception handler is started. This search inspects the
except clauses in turn until one is found that matches the
exception. [...]
So, all you need to do is put the socket.timeout except clause before
any socket.error clause:
try:
self.data = self.page.read()
except socket.timeout: ...
except socket.error,e: ...
/Bob
More information about the Python-list
mailing list