[Python-Dev] Socket timeout patch

Steve Holden sholden@holdenweb.com
Fri, 7 Jun 2002 14:19:18 -0400

[ ... ]
> > - The original timeout socket code (in Python) by Tim O'Malley had a
> >   global timeout which you could set so that *all* sockets
> >   *automatically* had their timeout set.  This is nice if you want it
> >   to affect library modules like urllib or ftplib.  That feature is
> >   currently missing.  Should we add it?  (I have some concerns about
> >   it, in that it might break other code -- and it doesn't seem wise to
> >   do this for server-end sockets in general.  But it's a nice hack for
> >   smaller programs.)
>   Is it really so painful for apps to keep track of all their sockets
> and then do something like:
>      for sock in sock_list:
>         sock.settimeout (blah)
>   Why keep track of them in the socket module, unless there's already code
> for this.
It isn't painful, it's impossible (unless you want to revise all the

The real problem comes when a program uses a socket-based library such as
smtplib or ftplib. Without the ability to impose a default timeout the
library client has no way to set a timeout until the library has created the
socket (and even then it will break encapsulation to do so in many cases).

Unfortunately, the most common requirement for a timeout is to avoid socket
code hanging when it makes the initial attempt to connect to a
non-responsive host. Under these circumstances, if the connect() doesn't
time out it can apparently be as long as two hours before an exception is


Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/