473,395 Members | 1,502 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,395 software developers and data experts.

Socket timeouts


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 = select.select([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
else:
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
other
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 select.select() 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 select.select 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

Jul 18 '05 #1
0 3292

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

Similar topics

10
by: Sheila King | last post by:
I'm doing DNS lookups on common spam blacklists (such as SpamCop..and others) in an email filtering script. Sometimes, because the DNS server that is resolving the looksup can go down, it is...
5
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...
6
by: roger beniot | last post by:
I have a program that launches multiple threads with a ThreadStart method like the following (using System.Net.Sockets.Socket for UDP packet transfers to a server): ThreadStart pseudo code: ...
2
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...
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
10
by: John Nagle | last post by:
Here are three network-related exceptions. These were caught by "except" with no exception type, because none of the more specific exceptions matched. This is what a traceback produced: 1....
1
by: John Nagle | last post by:
Does setting "socket.setdefaulttimeout" affect the timeout in MySQLdb for connections to the database? I'm getting database connection timeouts on a local (same machine) connnection, and I've been...
10
by: Hendrik van Rooyen | last post by:
While doing a netstring implementation I noticed that if you build a record up using socket's recv(1), then when you close the remote end down, the recv(1) hangs, despite having a short time out...
0
by: John Nagle | last post by:
What actually works and what doesn't in socket timeouts in Python 2.5? What breaks with timeouts turned on? There are lots of patches and bug reports, but no coherent summary. John Nagle
10
by: ColoradoGeiger | last post by:
I have a fairly standard server application that I am using asynchronous socket calls to make connections, send and receive data, and all of that jazz. No blocking methods was my goal in writing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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:
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...
0
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,...
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...
0
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...

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.