How to catch socket timeout?

Mickel Grönroos mickel at csc.fi
Wed Sep 24 09:15:41 EDT 2003


On Wed, 24 Sep 2003, Mickel Grönroos wrote:

> On Wed, 24 Sep 2003, Michael Hudson wrote:
>
> > Paul <paul at oz.net> writes:
> >
> > > What's the best way to do this under Python 2.1?
> > > I believe socket.timeout was a 2.3 feature.
> > > Wrap it in threads?
> >
> > There's Tim O'Malley's timeoutsocket.py (google for it), which was the
> > inspiration for the feature in 2.3 (though I don't think any code from
> > there actually survived).
>
> I can't get the timeoutsocket module to work on my Redhat Linux! (The
> timeout works fine on Windows 2000.) I'm running Python 2.2.2.
>
> Here's the test code:
>
> import timeoutsocket
> timeoutsocket.setDefaultSocketTimeout(1)
> s = timeoutsocket.socket(timeoutsocket.AF_INET, timeoutsocket.SOCK_STREAM)
> s.connect(("www.google.com", 80))
> s.close()

I might add, that I conducted the test by running the above code
with and without my Ethernet cable plugged to the computer. When it was
plugged connect() worked just fine, when unplugged, the connect() call
timed out on Windows as expected but not at all on Linux.

/Mickel G.

--
Mickel Grönroos, application specialist, linguistics, Research support, CSC
PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
CSC is the Finnish IT center for science, www.csc.fi





More information about the Python-list mailing list