好吧,可能是名称解析花了你这么长时间。如果你把它排除在外(即,如果 dig 以某种方式很快返回),Python 应该能够轻松处理数千个条目。
也就是说,您应该尝试线程方法。这将(理论上)同时解析多个地址,而不是顺序解析。您也可以继续使用 dig ,并且为此修改下面的示例代码应该很简单,但是,为了让事情变得有趣(并且希望更 Pythonic),让我们为此使用现有模块:dnspython
所以,安装它:
sudo pip install -f http://www.dnspython.org/kits/1.8.0/ dnspython
然后尝试以下操作:
import threading
from dns import resolver
class Resolver(threading.Thread):
def __init__(self, address, result_dict):
threading.Thread.__init__(self)
self.address = address
self.result_dict = result_dict
def run(self):
try:
result = resolver.query(self.address)[0].to_text()
self.result_dict[self.address] = result
except resolver.NXDOMAIN:
pass
def main():
infile = open("domainlist", "r")
intext = infile.readlines()
threads = []
results = {}
for address in [address.strip() for address in intext if address.strip()]:
resolver_thread = Resolver(address, results)
threads.append(resolver_thread)
resolver_thread.start()
for thread in threads:
thread.join()
outfile = open('final.csv', 'w')
outfile.write("\n".join("%s,%s" % (address, ip) for address, ip in results.iteritems()))
outfile.close()
if __name__ == '__main__':
main()
如果这证明同时启动太多线程,您可以尝试分批执行,或使用队列(参见http://www.ibm.com/developerworks/aix/library/au-threadingpython/示例)