socket timeouts

Michael P. Reilly arcege at shore.net
Wed Jun 30 14:49:18 EDT 1999


Nathan Clegg <nathan at islanddata.com> wrote:
: Is there any way to set the timeout for a TCP/IP connection? 
: Specifically, I'm writing some apps meant to run on the same machine, so
: if they can't connect *immediately* (i.e. the server app is not running), I
: need to go to plan B.  Any answers or alternate solutions?  I'm also
: interested in a similar issue across the internet, but I'm more generous
: about timeouts in this circumstance.  Thanks.

Since you are using a TCP/IP connection (ie. a socket), using select()
should be cross-platform enough.

  import select
  class TimeoutError(Exception):
    pass
  def wait_for_socket(sock, timeo):
    timeo = float(timeo)
    rdrs, wtrs, excs = select.select([sock], [], [], timeo)
    if not rdrs:
      raise TimeoutError, "no responce"

If the exception is raised, then the connection did not complete.

  -Arcege





More information about the Python-list mailing list