473,543 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:\CrawlingFra mework\Rules\To ols\__init__.py ", line 91, in __init__
self.data = self.page.read( )
File "C:\Python23\li b\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 52112
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=1 00"
socket.setdefau lttimeout(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?wai t=10"
socket.setdefau lttimeout(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?wai t=10"
socket.setdefau lttimeout(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***@procoder s.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******** *************** ***********@pyt hon.org...
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******** *************** ***********@pyt hon.org...
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=1 00"
socket.setdefau lttimeout(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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1591
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 prog/script? eg: import smtplib import (other stuff)
0
1795
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 file or a zip file the response is "Object are closed in the middle of the SSL handshake" and I receive half file. Instead if I use another kind of...
1
4543
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 remote side closes the connection. Thanks in advance, Franky
0
2789
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 2005, 15:19:29)
1
1741
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 the socket read and write with a thread. However, I have tried this and have a problem where ONLY on linux does the program crash, it DOES NOT...
5
8408
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 fsockopen to connect. $timeout-=(time()-$start);
2
2793
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 this error in fact seemed to come from the os (in this case windows xp). By inspection of test cases the error.reason from the deliberate socket...
2
3523
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 timeout.struct_to_timeout(self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeout.struct_size())) The helper timeout module is here:
0
7341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7729
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7332
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5258
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4884
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3385
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1809
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
626
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.