[Python-Dev] TCP socket timeout module?

Peter Schneider-Kamp nowonder@nowonder.de
Mon, 31 Jul 2000 10:07:46 +0000


How hard would it be to implement a functionality like this
in socketmodule.c?

Is such a functionality desirable?

guess-this-has-been-discussed-a-thousand-times-before-ly y'rs
Peter

-------- Original Message --------
Subject: [ANN] TCP socket timeout module --> timeoutsocket.py
Date: Sun, 30 Jul 2000 20:54:23 -0400
From: "Timothy O'Malley" <timo@alum.mit.edu>
Organization: Posted via Supernews, http://www.supernews.com
To: python-list@python.org
Newsgroups: comp.lang.python


Numerous times I have seen people request a solution for TCP socket
timeouts in conjunction with urllib.  Recently, I found myself in the
same boat.  I wrote a server that would connect to skytel.com and send
automated pages.  Periodically, the send thread in the server would
hang for a long(!) time.  Yup -- I was bit by a two hour timeout built
into tcp sockets.

Thus, I wrote timeoutsocket.py

With timeoutsocket.py, you can force *all* TCP sockets to have a
timeout.  And, this is all accomplished without interfering with the
standard python library!

Here's how to put a 20 second timeout on all TCP sockets for urllib:

   import timeoutsock
   import urllib
   timeoutsocket.setDefaultSocketTimeout(20)

Just like that, any TCP connection made by urllib will have a 20 second
timeout.  If a connect(), read(), or write() blocks for more than 20
seconds, then a socket.Timeout error will be raised.

Want to see how to use this in ftplib?

   import ftplib
   import timeoutsocket
   timeoutsocket.setDefaultSocketTimeout(20)

Wasn't that easy!
The timeoutsocket.py module acts as a shim on top of the standard
socket module.  Whenever a TCP socket is requested, an instance of
TimeoutSocket is returned.  This wrapper class adds timeout support to
the standard TCP socket.

Where can you get this marvel of modern engineering?

   http://www.timo-tasi.org/python/timeoutsocket.py

And it will very soon be found on the Vaults of Parnassus.

Good Luck!

-- 
--
happy daze
  -tim O
-- 
http://www.python.org/mailman/listinfo/python-list