[Tutor] Socket Timeout Handling

wormwood_3 wormwood_3 at yahoo.com
Thu Sep 6 15:40:21 CEST 2007


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("64.33.212.2")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '_socketobject' object has no attribute 'getfqdn'
>>> socket.getfqdn("64.33.212.2")
'64-33-212-2.customers.pingtone.net'
>>> # Disconnected network connection here
... 
>>> socket.getfqdn("64.33.212.2")
'64.33.212.2'
>>> # Reconnected network connection here
>>> socket.getfqdn("64.33.212.2")
'64-33-212-2.customers.pingtone.net'

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.

-Sam




More information about the Tutor mailing list