Mailing List Archive

socket timeouts
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.


----------------------------------
Nathan Clegg
nathan@islanddata.com
socket timeouts [ In reply to ]
From: Nathan Clegg <nathan@islanddata.com>

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.


----------------------------------
Nathan Clegg
nathan@islanddata.com
socket timeouts [ In reply to ]
Nathan Clegg <nathan@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
socket timeouts [ In reply to ]
From: "Michael P. Reilly" <arcege@shore.net>

Nathan Clegg <nathan@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
socket timeouts [ In reply to ]
Nathan Clegg writes:
> 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.

What type of platform are you running on? On my Linux 2.2.10 box, a
"connect" to a port on the local host with no server app fails
immediately with a "connection refused".