gpt4 book ai didi

python : Speed up Reverse DNS lookup

转载 作者:太空宇宙 更新时间:2023-11-03 17:35:01 27 4
gpt4 key购买 nike

我计划在 4700 万个 ip 上运行反向 DNS。这是我的代码

with open(file,'r') as f:
with open ('./ip_ptr_new.txt','a') as w:

for l in f:

la = l.rstrip('\n')
ip,countdomain = la.split('|')
ips.append(ip)

try:
ais = socket.gethostbyaddr(ip)
print ("%s|%s|%s" % (ip,ais[0],countdomain), file = w)
except:
print ("%s|%s|%s" % (ip,"None",countdomain), file = w)

目前速度非常慢。有人有任何加快速度的建议吗?

最佳答案

尝试使用多处理模块。我对大约 8000 ips 的性能进行了计时,得到了以下结果:

#dns.py
real 0m2.864s
user 0m0.788s
sys 0m1.216s


#slowdns.py
real 0m17.841s
user 0m0.712s
sys 0m0.772s


# dns.py
from multiprocessing import Pool
import socket
def dns_lookup(ip):
ip, countdomain = ip
try:
ais = socket.gethostbyaddr(ip)
print ("%s|%s|%s" % (ip,ais[0],countdomain))
except:
print ("%s|%s|%s" % (ip,"None",countdomain))

if __name__ == '__main__':
filename = "input.txt"
ips = []
with open(filename,'r') as f:
with open ('./ip_ptr_new.txt','a') as w:
for l in f:
la = l.rstrip('\n')
ip,countdomain = la.split('|')
ips.append((ip, countdomain))
p = Pool(5)
p.map(dns_lookup, ips)





#slowdns.py
import socket
from multiprocessing import Pool

filename = "input.txt"
if __name__ == '__main__':
ips = []
with open(filename,'r') as f:
with open ('./ip_ptr_new.txt','a') as w:
for l in f:
la = l.rstrip('\n')
ip,countdomain = la.split('|')
ips.append(ip)
try:
ais = socket.gethostbyaddr(ip)
print ("%s|%s|%s" % (ip,ais[0],countdomain), file = w)
except:
print ("%s|%s|%s" % (ip,"None",countdomain), file = w)

关于 python : Speed up Reverse DNS lookup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31316789/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com