[Python-Dev] timeout support for socket.py? (was: [ANN] TCP socket timeout module -->
timeoutsocket.py)
Peter Schneider-Kamp
nowonder@nowonder.de
Thu, 17 Aug 2000 07:13:08 +0000
As the socketmodule is now exported as _socket and a seperate socket.py
file wrapping _socket is now available in the standard library,
wouldn't it be possible to include timeout capabilities like in
http://www.timo-tasi.org/python/timeoutsocket.py
If the default behaviour would be "no timeout", I would think this
would not break any code. But it would give an easy(and documentable)
solution) to people who e.g. have their
urllib.urlopen("http://spam.org").read()
hang on them. (Actually the approach should work for all streaming
socket connections, as far as I understand it.)
Are there any efficiency concerns? If so, would it be possible to
include a second socket class timeoutsocket in socket.py, so that
this could be used instead of the normal socket class? [In this case
a different default timeout than "None" could be chosen.]
Peter
P.S.: For your convenience a quote of the announcement on c.l.py,
for module documentation (== endlessly long doc string) look in
http://www.timo-tasi.org/python/timeoutsocket.py
Timothy O'Malley wrote:
>
> 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
--
Peter Schneider-Kamp ++47-7388-7331
Herman Krags veg 51-11 mailto:peter@schneider-kamp.de
N-7050 Trondheim http://schneider-kamp.de