473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 52077
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 socket.setdefaulttimeout() value used by the rest of 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 there is no problem instead if the client ask a pdf...
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 is blocked at the socket's EndSend step until the...
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 track down this problem: Python 2.4.1 (#1, May 16...
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 times out. I know that a good solution is to have...
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 $timeout by the amount of time that it took for...
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 socket.setdefaulttimeout(120). Closer inspection revealed that...
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 example: def get_socket_read_timeout(self): return...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.