469,268 Members | 920 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

How to catch socket timeout?

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
Jul 18 '05 #1
17 51065
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

Jul 18 '05 #2
"Peter Otten" <__*******@web.de> wrote in message
news:bk*************@news.t-online.com...
So it seems there is *no* way to catch the error.
I think you should file a bug report.


Where/How do I do that?

Achim
Jul 18 '05 #3
Peter Otten wrote:
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,

--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
Jul 18 '05 #4
Achim Domma wrote:
So it seems there is *no* way to catch the error.
I think you should file a bug report.


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
Jul 18 '05 #5
Alan Kennedy wrote:
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
Jul 18 '05 #6
"Achim Domma" <do***@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

Jul 18 '05 #7
"Bob Halley" <ha****@play-bow.org> wrote in message
news:ma**********************************@python.o rg...
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
Jul 18 '05 #8
Achim Domma wrote:
"Bob Halley" <ha****@play-bow.org> wrote in message
news:ma**********************************@python.o rg...
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


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

Jul 18 '05 #9
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

Jul 18 '05 #10
Paul <pa**@oz.net> writes:
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

--
31. Simplicity does not precede complexity, but follows it.
-- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html
Jul 18 '05 #11
Paul <pa**@oz.net> writes:
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

--
31. Simplicity does not precede complexity, but follows it.
-- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html
Jul 18 '05 #12
Ed
Have a look at:

http://www.nongnu.org/bothans/pydoc/...outsocket.html

Paul wrote:
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


Jul 18 '05 #13
Ed
Have a look at:

http://www.nongnu.org/bothans/pydoc/...outsocket.html

Paul wrote:
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


Jul 18 '05 #14
On Wed, 24 Sep 2003, Michael Hudson wrote:
Paul <pa**@oz.net> writes:
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).


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.

Jul 18 '05 #15
On Wed, 24 Sep 2003, Michael Hudson wrote:
Paul <pa**@oz.net> writes:
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).


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.

Jul 18 '05 #16
On Wed, 24 Sep 2003, Mickel Grönroos wrote:
On Wed, 24 Sep 2003, Michael Hudson wrote:
Paul <pa**@oz.net> writes:
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).


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.

--
Mickel Grönroos, application specialist, linguistics, Research support,CSC
PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
CSC is the Finnish IT center for science, www.csc.fi

Jul 18 '05 #17
On Wed, 24 Sep 2003, Mickel Grönroos wrote:
On Wed, 24 Sep 2003, Michael Hudson wrote:
Paul <pa**@oz.net> writes:
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).


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.

--
Mickel Grönroos, application specialist, linguistics, Research support,CSC
PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
CSC is the Finnish IT center for science, www.csc.fi

Jul 18 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Tim Williams | last post: by
reply views Thread by Paola | last post: by
1 post views Thread by Franky | last post: by
reply views Thread by Jaap Spies | last post: by
1 post views Thread by Kiran | last post: by
5 posts views Thread by yawnmoth | last post: by
2 posts views Thread by Robin Becker | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.