练习:多线程对Ip进行tcp端口探测
import threading
import socket
imoprt time
#获得扫描ip
def getip():
iplist = []
o = open('ip.txt','r') #现在是从文件中取出ip
for ip in o.readlines():
a = ip.strip()
iplist.append(a)
o.close()
return iplist
#扫描函数
def scan(ip):
s = socket(socket.AF_INET, socket.SOCK_STREAM)
for port in xrange(1,65535):
try:
s.connect((ip,port))
print 'open port!!'+ ip + '\t' + str(port)
except:Exception,e:
pass
#使用多线程进行扫描
def main():
for ip in getip():#将每个参数生成一个对应的线程
t = threading.Thread(target=scan,args=(ip,))
threads.append(t)
n = 0
while True:
if n == len(threads)
break
thread = threads[n]
if threading.activeCount() < 10:
print 'started ' + str(thread)
thread.start()
thread.join()
else:
n -=1
n += 1
print n
time.sleep(2)
''' for thread in threads:
if threading.activeCount() < 2:
thread.start()
thread.join()
print 'started: ' + str(thread)
else:
pass
'''
if _name_ == "_main_):
main()