473,499 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

deliberate versus os socket timeout

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 timeout
looks like
'timed out'
whereas the windows caused timeout error.reason looks like
'(10060 operation timed out)'

it would be nice to know if that is in fact true and whether there is any way to
do the attribution of errors more sensibly.

Both of these seem to cause urllib2.URLError and presumably appear somewhere in
the socket code.

It might be nice if the deliberate timeout could be something like 'timed out
deliberately after xxx seconds'.

More importantly is there anything I can do to avoid these wrong os inspired
timeouts? I'm just using urllib2 to read from a remote site inside of a cgi
script. I'm not sure it's a big problem, but I have no idea what could cause it.

The code I'm using is based on one of jjlee's recipes

def httpGet(self):
import urllib2, socket
from xml.sax.saxutils import escape
oto = socket.getdefaulttimeout()
try:
socket.setdefaulttimeout(self.timeout)
self.url = url = self.makeUrl(self.params)
try:
self.response = r = urllib2.urlopen(url)
except urllib2.URLError, e:
if hasattr(e, 'reason'):
msg = 'HTTP error '+str(e.reason)
if 'time' in msg:
msg += '(our timeout=%s)' % socket.getdefaulttimeout()
return 1,escape(msg)
elif hasattr(e, 'code'):
return 1, escape('The server couldn\'t fulfill the
request.\nError code: '+str(e.code))
else:
# everything is fine
h = self.headers = {}
........
return 0,......
finally:
socket.setdefaulttimeout(oto)

--
Robin Becker

Jul 5 '07 #1
2 2790
Robin Becker <ro***@reportlab.comwrites:
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 timeout looks like
'timed out'
whereas the windows caused timeout error.reason looks like
'(10060 operation timed out)'

it would be nice to know if that is in fact true and whether there is
any way to do the attribution of errors more sensibly.
What do you mean "is in fact true"? Is what true? That the reason is
always formatted in those ways when those particular errors occur?
Not sure there's any reason to rely on that: I'd expect .reason to be
an exception object, so (reading the docs for module socket)
e.reason.args[0] should be the error code if one is available, in this
case errno.ETIMEDOUT (i.e. WSAETIMEDOUT, which means the timeout
occurred before a connection was established). Sometimes
e.reason.args will be length-1, and e.reason.args[0] will be a string,
again according to the docs for module socket.

Both of these seem to cause urllib2.URLError and presumably appear
somewhere in the socket code.

It might be nice if the deliberate timeout could be something like
timed out deliberately after xxx seconds'.
By "deliberate timeout", you mean the one you asked for by calling
socket.setdefaulttimeout().

In that case, I think e.reason will be a socket.timeout instance (as
opposed to socket.error in the WSAETIMEOUT case).

More importantly is there anything I can do to avoid these wrong os
inspired timeouts? I'm just using urllib2 to read from a remote site
[...]

Please define "wrong" ;-)

Googling suggests Windows doesn't let you configure its connect
timeout, except through a registry setting:

http://www.tech-archive.net/Archive/.../msg00918.html
John
Jul 5 '07 #2
John J. Lee wrote:
>
What do you mean "is in fact true"? Is what true? That the reason is
always formatted in those ways when those particular errors occur?
Not sure there's any reason to rely on that: I'd expect .reason to be
an exception object, so (reading the docs for module socket)
e.reason.args[0] should be the error code if one is available, in this
case errno.ETIMEDOUT (i.e. WSAETIMEDOUT, which means the timeout
occurred before a connection was established). Sometimes
e.reason.args will be length-1, and e.reason.args[0] will be a string,
again according to the docs for module socket.
well I suspect there's a whole twisty maze of stuff to do to reliably detect
whether a timeout was deliberate or not so I'll pass on that.

..........
>
>More importantly is there anything I can do to avoid these wrong os
inspired timeouts? I'm just using urllib2 to read from a remote site
[...]

Please define "wrong" ;-)
well if I ask to be timed out after 120 seconds I don't expect a timeout error
after 20 so I would define the 20 second case to be somehow wrong or unexpected.
Googling suggests Windows doesn't let you configure its connect
timeout, except through a registry setting:

http://www.tech-archive.net/Archive/.../msg00918.html
the implication of windows providing its own timeout seems to make the socket
defaulttimeout irrelevant since I can have no per socket control over it. I
think this came up before and I just forgot about it mumble mumble
--
Robin Becker

Jul 6 '07 #3

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

Similar topics

17
52104
by: Achim Domma | last post by:
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:
3
2639
by: Thomas Hervé | last post by:
My problem is not really python specific but as I do my implementation in python I hope someone here can help me. I have two programs that talk through a socket. Here is the code : <server>...
5
4205
by: Russell Warren | last post by:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it a cross-process/system setting or does it stay local in the application in which it is called? I've been testing this and...
3
4611
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, I use the System.Net.Sockets to send/receive data (no tcpclient/tcplistener), I made a receivethread in my wrapper, the receivethread loops/sleeps while waiting for data...
4
18084
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
2
15298
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
1
4424
by: jnair | last post by:
When using socket.socket.settimeout we normally only guard against "socket.timeout" exception.Now the implementation of "settimeout" in "Python-2.4.3/Modules/socketmodule.c" sets the socket fd to...
2
10655
by: carl.rosenberger | last post by:
Hi, I am trying to get the following behaviour for my Socket connection: (1) Attempt a blocked read for a defined amount of time. (2) If a timeout occurs, because no data has been sent to the...
2
3520
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
7134
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
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6901
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...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4920
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...
0
4605
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...
0
3105
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...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
307
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...

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.