How to catch socket timeout?

A

Achim Domma

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
 
P

Peter Otten

Achim said:
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
 
P

Peter Otten

Achim said:
Where/How do I do that?

http://www.python.org/dev has an outside link to Bug Tracker leading to a
Sourceforge page where you can submit a short description of the bug.

Have a look at the list of bugs both to see if your bug has already been
submitted by others as well as for example submissions.


Peter
 
A

Alan Kennedy

Peter said:
socket.timeout is a subclass of socket.error, so the timeout exception
should be caught by the first except clause.
So it seems there is *no* way to catch the error.
I think you should file a bug report.

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,
 
P

Peter Otten

Alan said:
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"
#==========================================================

You are right. I did not read the traceback carefully.

Peter
 
B

Bob Halley

Achim Domma said:
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
 
A

Achim Domma

Bob Halley said:
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: ...

Thanks, that works fine, but I don't understand why the exception was not
catched in my case. If I write it like this

try:
self.data = self.page.read()
except socket.error,e: ...
except socket.timeout: ...

the exception should be catched by the socket.error handler. Or am I wrong?
In my case it was not catched at all. Very mysterious from my point of view,
but it works now.

Achim
 
P

Peter Otten

Achim said:
Thanks, that works fine, but I don't understand why the exception was not
catched in my case. If I write it like this

try:
self.data = self.page.read()
except socket.error,e: ...
except socket.timeout: ...

the exception should be catched by the socket.error handler. Or am I
wrong? In my case it was not catched at all. Very mysterious from my point
of view, but it works now.

Achim

Achim, both you and me got it wrong the first time. The important part is to
put both urlopen() and page.read() into the try clause, as Alan Kennedy has
already shown. Both statements can throw a timeout exception, so it's sheer
luck that it worked this time.
And yes, if you put

except socket.error:

before

except socket.timeout:

the latter will never be executed. General structure:


from urllib2 import urlopen
import socket

slowurl = "http://127.0.0.1/timeout?delay=100"
socket.setdefaulttimeout(1)

try:
data = urlopen(slowurl)
data.read()
except socket.timeout:
print "Timeout raised and caught"

Peter
 
P

Paul

What's the best way to do this under Python 2.1?
I believe socket.timeout was a 2.3 feature.
Wrap it in threads?

I'd like to do something similar for the Gibraltar
Linux firewall CD, but it only comes with Python 2.1.

Many thanks in advance.

-- Paul
 
M

Michael Hudson

Paul said:
What's the best way to do this under Python 2.1?
I believe socket.timeout was a 2.3 feature.
Wrap it in threads?

There's Tim O'Malley's timeoutsocket.py (google for it), which was the
inspiration for the feature in 2.3 (though I don't think any code from
there actually survived).

Cheers,
mwh
 
?

=?ISO-8859-1?Q?Mickel_Gr=F6nroos?=

There's Tim O'Malley's timeoutsocket.py (google for it), which was the
inspiration for the feature in 2.3 (though I don't think any code from
there actually survived).

I can't get the timeoutsocket module to work on my Redhat Linux! (The
timeout works fine on Windows 2000.) I'm running Python 2.2.2.

Here's the test code:

import timeoutsocket
timeoutsocket.setDefaultSocketTimeout(1)
s = timeoutsocket.socket(timeoutsocket.AF_INET, timeoutsocket.SOCK_STREAM)
s.connect(("www.google.com", 80))
s.close()

Any ideas?

/Mickel G.
 
?

=?ISO-8859-1?Q?Mickel_Gr=F6nroos?=

I can't get the timeoutsocket module to work on my Redhat Linux! (The
timeout works fine on Windows 2000.) I'm running Python 2.2.2.

Here's the test code:

import timeoutsocket
timeoutsocket.setDefaultSocketTimeout(1)
s = timeoutsocket.socket(timeoutsocket.AF_INET, timeoutsocket.SOCK_STREAM)
s.connect(("www.google.com", 80))
s.close()

I might add, that I conducted the test by running the above code
with and without my Ethernet cable plugged to the computer. When it was
plugged connect() worked just fine, when unplugged, the connect() call
timed out on Windows as expected but not at all on Linux.

/Mickel G.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,177
Latest member
OrderGlucea
Top