forked from meizhitu/100programhomework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
100-38-network-6.py
62 lines (53 loc) · 1.66 KB
/
100-38-network-6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#coding=utf-8
from socket import *
from threading import Thread
from Queue import Queue
def scan(ip,port):
s=socket(AF_INET,SOCK_STREAM)
s.settimeout(0.5)
result=s.connect_ex((ip,port))
if(result==0):
print 'Port %d: OPEN' % port
s.close()
class Worker(Thread):
def __init__(self,taskQueue):
Thread.__init__(self)
self.setDaemon(True)
self.taskQueue=taskQueue
self.start()
def run(self):
while True:
try:
callable,args,kwds=self.taskQueue.get(block=False)
callable(*args,**kwds)
except:
break
class ThreadPool:
def __init__(self,ip):
self.threads=[]
self.taskQueue=Queue()
self.threadNum=10
self.__create_taskqueue(ip)
self.__create_threadpool(self.threadNum)
def __create_taskqueue(self,ip):
for i in range(20,10000):
self.add_task(scan,ip,i)
def __create_threadpool(self,threadNum):
for i in range(threadNum):
thread=Worker(self.taskQueue)
self.threads.append(thread)
def add_task(self,callable,*args,**kwds):
self.taskQueue.put((callable,args,kwds))
def waitfor_complete(self):
while len(self.threads):
thread=self.threads.pop()
thread.join()
if thread.isAlive() and not self.taskQueue.empty():
self.threads.append(thread)
print 'scaning is over!'
if __name__=='__main__':
target=raw_input('Enter host to scan:')
targetIP=gethostbyname(target)
print 'Starting scan on host',targetIP
tp=ThreadPool(targetIP)
tp.waitfor_complete()