[Tutor] Socket Timeout Handling

wormwood_3 wormwood_3 at yahoo.com
Thu Sep 6 22:46:08 CEST 2007

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

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? Something else?

I found some good general info here: http://www.onlamp.com/pub/a/python/2003/11/06/python_nio.html

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


----- Original Message ----
From: wormwood_3 <wormwood_3 at yahoo.com>
To: Python Tutorlist <tutor at python.org>
Sent: Thursday, September 6, 2007 9:40:21 AM
Subject: [Tutor] Socket Timeout Handling

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). 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. This means (as I understand it thus far), that while I can set a timeout value for socket objects, this will not apply to when I use the getfqdn() method, which is where I need a timeout check! Some example code for the steps so far:

>>> import socket
>>> conn = socket.socket()
>>> conn.setdefaulttimeout(2.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '_socketobject' object has no attribute 'setdefaulttimeout'
>>> socket.setdefaulttimeout(2.0)
>>> conn.getfqdn("")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '_socketobject' object has no attribute 'getfqdn'
>>> socket.getfqdn("")
>>> # Disconnected network connection here
>>> socket.getfqdn("")
>>> # Reconnected 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? 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?

Thanks for any help.


Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list