socket setdefaulttimeout

Steve Holden steve at holdenweb.com
Wed Aug 17 06:27:55 EDT 2005


Sheila King wrote:
> I'm doing DNS lookups on common spam blacklists (such as SpamCop..and
> others) in an email filtering script. Sometimes, because the DNS server
> that is resolving the looksup can go down, it is important to make sure
> that the socket doesn't just hang there waiting for a response.
> 
> After a recent system upgrade to Python 2.4.1 (from 2.2.2) I thought I
> could take advantage of the setdefaulttimeout in the socket module, to
> limit the amount of time the sockets take for a lookup.
> 
> As a test, I set the default timout ridiculously low. But it doesn't
> seem to be having any effect. The sockets will take several seconds to
> make the connection and complete the lookups, even though I've set the
> timeout to millionths of a second, which I thought would ensure a
> timeout (sample script below).
> 
> Am I doing something wrong? Do I misunderstand something? Is what I
> want to do simply not possible?
> 
> Thanks for any tips. Example code follows signature...
> 
> --
> Sheila King
> http://www.thinkspot.net/sheila/
> 
> #!/usr/local/bin/python2.4
> import socket
> import sys
> from time import time, asctime, localtime
> 
> socket.setdefaulttimeout(.00001)
> debugfile = "socketdebug.txt"
> 
> 
> def debug(data):
>     timestamp = str(asctime(localtime(time())))
>     try:
>         f = open(debugfile, 'a')
>         f.write('\n*** %s ***\n' % timestamp)
>         f.write('%s\n' % data)  # 24-Dec-2003 -ctm- removed one
> linefeed
>         f.close()
>     except IOError:
>         pass
>         # do nothing if the file cannot be opened
> 
> 
> IPaddy = '220.108.204.114'
> 
> if IPaddy:
>     IPquads = IPaddy.split('.')
>     IPquads.reverse()
>     reverseIP = '.'.join(IPquads)
> 
>     bl_list = { 'bl.spamcop.net' : 'IP Address %s Rejected - see:
> http://spamcop.net/bl.shtml' % IPaddy, \
>                 'relays.ordb.org' : 'IP Address %s Rejected - see:
> http://ordb.org/' % IPaddy, \
>                 'list.dsbl.org' : 'IP Address %s Rejected - see:
> http://dsbl.org' % IPaddy}
> 
>     timing_done = 0
>     start_time = time()
>     for host in bl_list.keys():
>         if host in bl_list.keys():
>             IPlookup = "%s.%s" % (reverseIP, host)
>             try:
>                 debug("  IPlookup=%s=" % IPlookup)
>                 resolvesIP = socket.gethostbyname(IPlookup)
>                 debug("  resolvesIP=%s=" % resolvesIP)
>                 if resolvesIP.startswith('127.'):
>                     end_time = time()
>                     elapsed_time = end_time - start_time
>                     timing_done = 1
>                     debug("Time elapsed for rDNS on bl_list: %f secs" %
> elapsed_time)
>                     debug("exiting--SPAM! id'd by %s" % host)
>                     print bl_list[host]
>                     sys.exit(0)
>             except socket.gaierror:
>                 pass
>     if not timing_done:
>         end_time = time()
>         elapsed_time = end_time - start_time
>         debug("2nd try:Time elapsed for rDNS on bl_list: %f secs" %
> elapsed_time)
> 
I don't believe that gethostbyname()'s use of socket technology can be 
expected to raise socket timeout exceptions, since in general it's a 
call to a library that uses standard system calls. This would at least 
explain the behaviour you were seeing.

It might just be easier to to the DNS work yourself using the rather 
nifty "dnspython" module. This does allow you to easily implement 
timeouts for specific interactions.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/




More information about the Python-list mailing list