[Tutor] Socket Timeout Handling

Alan Gauld alan.gauld at btinternet.com
Fri Sep 7 01:41:32 CEST 2007

"wormwood_3" <wormwood_3 at yahoo.com> wrote

> Since no one bit on this yet, let me simplify to the core issue I am 
> having:

That may be because your question ventures into fairly deep areas of 
that most folk who are just learning Python(ie readers of this list) 
have probably
not encountered. I've used python for network programming but not 
and certainly not for hard core production use so can't help you.

Similarly I've used sockets extesively from C/C++ but I'm  not sure 
how well
that transfers to Python without doing more digging than I hsave time 
for right

If you do want to get into depth on Python networking you may find the
A-Press book Python Network Programming useful - I do regularly :-).

However the only information I could see about timeouts there was
related to socket.settimeout() which ISTR you didn't want to/couldn't 

> What is the best practice for checking for network connectivity
> errors when making network calls? Is it better to wrap the functions
> that make said calls in threads and time them?
> Or to use timeout variables for modules like socket?

Personally if i was doingt that I'd almost certainy put it in a thread
and apply a timeout within the thread. but not having tried that I 
know how easy it would be!

> But I have had a hard time finding info on network error handling 
> specifically.

The A-Press book does devote several pages in total to socket error
handling including connection errors, timeout errors and transmission 

> I am trying to figure out the optimal way to make socket connections
> (INET) and check for timeouts. The socket module has 
> settimeout(timeout)
> and setdefaulttimeout(timeout). However, so far as I can tell, these 
> apply
> to socket objects. The type of socket connection I want to make is
> getfqdn(address).

I don't understand, getfqdn() returns a domain name not an a socket?

> So I can set the default timeout for socket, but not a socket object
> (makes sense so far). I cannot use the getfqdn(address) method on
> a socket object, I have to use it on socket.

Sorry it's not making sense to me, getfqdn takes a host name not a 

>>>> import socket
>>>> conn = socket.socket()
>>>> conn.setdefaulttimeout(2.0)

setdefaulttimout is a function in the socket module not a method of 
Thus you woulfd call that before reating a new socket:

>>> socket.setdefaulttimeout(5)   # set default t/o of 5 secs
>>> conn = socket.socket()        # with timeout of 5

>>>> socket.setdefaulttimeout(2.0)
>>>> conn.getfqdn("")

Again getfqdn is a function in the socket module not a method.
But if you give it an IP saddress it will just return that IP address!

>>>> socket.getfqdn("")
> '64-33-212-2.customers.pingtone.net'

OK, Apparently it will give you more... :-)

>>>> # Disconnected network connection here
> ...
>>>> socket.getfqdn("")
> ''

> After I disconnected my network connection and called getfqdn(),
> it returned the IP address I called it with after about 25 seconds.
> So the default timeout was ignored?

Yes because there was no socket being created it was doing a
name lookup using standard DNS etc, and with the network disconnected
timed out at the OS level I suspect. If you want to control the DNS
lookup you will need to cofde that manually I suspect. (As I say,
this is way deeper than I've needed to peer into these type of
operations, the defaults have worked for me!)

> Is there some other way to call this function so that I can check
> for timeouts? Should I instead just put my network calls in a
> thread and see how long they take, stopping them after a certain 
> period?

I don't think that would necessarily help here.

What are you trying to do? Establish a socket connection to something
or just do a name check that times out more quickly?(or slowly)

> Thanks for any help.

Not sure how much help, not even sure I understand what you are
up to, but it might spark an idea...

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list