472,330 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,330 software developers and data experts.

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 51709
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tim Williams | last post by:
(python newbie) Is it possible to individually set the socket timeout of a connection created by smtplib, rather than use the...
0
by: Paola | last post by:
Hi, I have a very strange problem with PowerTCP: I made a wrapper between a server web and the browsers. If the client ask Html pages or images...
1
by: Franky | last post by:
Hello, How can I set timeout on the send for an asynchronous socket? I use the setsocketoption's timeout but it does not seem work. The program...
0
by: Jaap Spies | last post by:
Hi, Running Fedora Core 4: Python 2.4.3 and Python 2.4.1. I'm getting: IOError: (2, 'No such file or directory') all the time. Trying to...
1
by: Kiran | last post by:
Hello All, I am creating a socket connection in order to read and write to a location. My problem is,the gui becomes unresponsive if the socket...
5
by: yawnmoth | last post by:
Say I have the following script: <? $timeout = 5; $start = time(); $fsock = fsockopen('125.1.119.10',80,$errno,$errstr,$timeout); // reduce...
2
by: Robin Becker | last post by:
While messing about with some deliberate socket timeout code I got an unexpected timeout after 20 seconds when my code was doing...
2
by: Heikki Toivonen | last post by:
M2Crypto has some old code that gets and sets socket timeouts in http://svn.osafoundation.org/m2crypto/trunk/M2Crypto/SSL/Connection.py, for...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.