我希望在短时间内进行大量反向 DNS 查找。我目前已经使用 socket.gethostbyaddr 和 concurrent.futures 线程池实现了异步查找,但仍然没有看到所需的性能。例如,脚本在 2500 个 IP 地址上完成大约需要 22 分钟。
我想知道是否有更快的方法来做到这一点,而无需求助于 adns-python 之类的东西。我发现这个http://blog.schmichael.com/2007/09/18/a-lesson-on-python-dns-and-threads/提供了一些额外的背景。
代码片段:
ips = [...]
with concurrent.futures.ThreadPoolExecutor(max_workers = 16) as pool:
list(pool.map(get_hostname_from_ip, ips))
def get_hostname_from_ip(ip):
try:
return socket.gethostbyaddr(ip)[0]
except:
return ""
我认为问题的一部分是许多 IP 地址没有解析和超时。我试过:
socket.setdefaulttimeout(2.0)
但它似乎没有效果。