timeoutsocket module help

Sheila King usenet at thinkspot.net
Tue Sep 17 21:11:51 EDT 2002

OK, I'm doing DNS lookups in a script I've written, to determine whether
certain IP addresses are listed in DNS BL lists (for blocking spammers).

Have been using the code I've written for a long time now (months) without
significant problems. However, someone has made the point to me, that my
code should technically have a default timeout on the socket that tries to
do the DNS lookup. And they are right, so I'm trying to modify my code.

So, I'm using Tim O'Malley's timeoutsocket module, or trying to. But I am
not having any success with this. In the following C&P from an interactive
session, it took 6 or 7 seconds to return the socket error, which is the
result I would expect in the case that the host name is not in the DNS list
(and in this case it is not, so this is a correct result). The problem is,
that I set the default time out (I thought???) to 0.02 seconds, but the
lookup went waaaayyyy past that and basically seemed to ignore the

>>> import sys
>>> import socket
>>> sys.path.append(r'j:\web\thinkspotwebsite\xthinkspot\emailfilter_test')
>>> import timeoutsocket
>>> timeoutsocket.setDefaultSocketTimeout(0.02)
>>> socket.gethostbyname('')
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in ?
gaierror: (7, 'getaddrinfo failed')

Now, I will note, that the comments in the timeoutsocket module indicate
that it works for TCP connections, and I'm not sure that TCP is being used
in the gethostbyname function, as it is my understanding that DNS should
usually be done by UDP instead. So, maybe in this case, using O'Malley's
timeoutsocket module is totally inappropriate and won't work.

In any case, I'm hoping to get some clues here.


Sheila King

More information about the Python-list mailing list