python中實現單線程綁定域名解析,具體方法如下:
import time
import socket
def ReadHost(file):
hosts=[]
...
return hosts
def SynResolve(fr):
hosts=ReadHost(fr)
IPs={}
for host in hosts:
try:
results=socket.getaddrinfo(host,None)
for result in results:
print host, result[4][0]
IPs[result[4][0]]=host
except Exception,e:
print e
file.close()
if __name__=='__main__':
start=time.time()
print 'starting at: ',start
SynResolve('host')
print 'ending at: ',time.time()-start