Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Socket timeouts

Thread Tools

Socket timeouts

Posts: n/a

Hi Python Gurus!

Here is a method I used before to receive data over a socket (with
Python 2.2):

SELECT_GRANULARITY = 0.1 # 0.1 seconds

def readdata(self,length,timeout):
res = ''
remain = length
lapsed = 0.0
while (remain > 0) and (lapsed < timeout):
i,o,e =[self.socket],[],[],SELECT_GRANULARITY)
lapsed = lapsed + SELECT_GRANULARITY
if i:
res = res + self.socket.recv(remain)
remain = length - len(res)
if remain > 0:
raise TimeOutError
return res

I used a very similar code to send data over a socket. Now in Python
2.3, we have
the wonderful settimeout() method. Here is my problem. Suppose that the
side (sender) is rather slow. I want to receive 1MB data in 20 seconds
(at most). Probably
the socket socket.recv() call will not return all data after the first
call. So my old method
continuously trying to receive data until the timeout arrives (or it
will return sooner if all data
was received). This worked flawlessly with Python 2.2. With the new api,
it is possible to
use settimeout() instead of the call. However, I think
that it will not return
all the 1MB data after the first call either. No matter what was the
timeout, it will give up
receiving when the input buffer becomes empty. So I still need to
receive data in a while loop.
What are the benefits and drawback of this solution with the new
settimeout() method?
I think in this case, it is not easier to use the new API, am I right?

I read the library reference documentation about compatibility. There is
not a word about
what platforms support the settimeout() method so I suppose all
platforms support this. As
opposed to which is not available on every OS. There is a
note on this:

"This module provides access to the select() and poll() functions
available in most operating systems."

It is not clear to me where it is supported.

There is an interesting question about send() and sendall(). It would be
much easier to use
settimeout() and sendall() for sending (rather than select() and several
send() operations).
I suspect that sendall() calls the send() method several times, but what
about the timeout
in this case? I'm not sure. Can somebody tell me if sendall() will
handle the timeout correctly or not?

This question do not arise with recv() because there is no recvall().
Actually, it would be nice
to have a recvall() method with a length parameter. Suggestions,
comments are welcome.

Thank you in advance,

Laci 1.0

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM
forms authentication timeouts and session timeouts =?Utf-8?B?SmFzb24=?= ASP .Net 0 06-22-2004 09:24 AM
Session-Timeouts and Idle-Timeouts on AS5300 and AS5800 Matt Cisco 1 02-17-2004 06:59 PM