#!/usr/bin/python
# encoding: utf-8
import time,sys
import socket
import threading
def ReadHost(file):
hosts=[]
f=open(file,'r')
for line in f:
hosts.append(line.strip('\n'))
f.close()
return hosts
class ThreadClass(threading.Thread):
def __init__(self,host):
threading.Thread.__init__(self)
self.host=host
def run(self):
global IPhost
try:
res=socket.getaddrinfo(self.host,None)
if mutex.acquire(1):
for re in res:
IPhost[self.host]=re[4][0]
mutex.release()
except Exception,e:
# pass
print self.host,e
def MulThreadResolve(fr):
start=time.ctime()
print 'starting MulThreadResolve at: ',start
hosts=ReadHost(fr)
threads=[]
for host in hosts:
t=ThreadClass(host)
threads.append(t)
cntHost=len(hosts)
for i in range(cntHost):
threads[i].start()
for i in range(cntHost):
threads[i].join()
print 'ending MulThreadResolve at :', time.ctime(),'\n'
if __name__=='__main__':
IPhost={}
mutex=threading.Lock()
MulThreadResolve('16.txt')
for k,v in IPhost.items():
print "\033[32m %-35s %-10s\033[0m" % (k,v)
转载于:https://blog.51cto.com/liuchw/1681492