high volume/speed gethostbyaddr how?

brueckd at tbye.com brueckd at tbye.com
Fri May 3 13:43:35 EDT 2002


On Fri, 3 May 2002, Dan Polak wrote:

> Gethostbyaddr blocks by default, this means that there is an appreciable 
> delay before it returns.
> If you need to lookup a fair number of hostnames gethostbyaddr is much 
> too slow.
> Using select or the asyncore library might be the way to do it, but I 
> don't really understand how to make that work.
> What is a good way to retrieve the names for a 100 hosts within a few 
> seconds?

Well, it really depends on what the true bottleneck is, but an easy 
solution would be something like:

import threading, socket
ipList = [... create a list of IP addresses ...]
ipAndHostList = []

def LookupThread():
  while 1:
    try:
      ip = ipList.pop()
      ipAndHostList.append((ip, socket.gethostbyaddr(ip)[0]))
    except IndexError:
      break

for i in range(3):
  threading.Thread(target=LookupThread).start()

If this provides appreciable speedup, vary the number of threads.

-Dave






More information about the Python-list mailing list