python 线程池扫描路径 py菜鸟 求指点怎么解决!
# -*- coding:utf-8 -*-import httplib
from threading import Thread
from Queue import Queue
import time
#工作线程
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,url,num):
self.threads = []
self.taskQueue = Queue()
self.threadNum = num
self.__create_taskqueue(url)
self.__create_threadpool(self.threadNum)
#初始化工作列队
def __create_taskqueue(self,url):
for i in range(500):
self.add_task(scanpath,url,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!'
#扫描路径
def scanpath(url):
var1 = 0
var2 = 0
f = open('webpath.txt','r')
for path in f:
path = path.replace("\n","")
path = "/" + path
target = url + path
print ("\t [#] Checking " + target + "...")
conn = httplib.HTTPConnection(url)
conn.request('GET',path)
result = conn.getresponse()
var1 = var1 + 1
ret = result.status
if ret == 200 or ret == 500 or ret == 403 or ret == 301:
var2 = var2 +1
print "%s %s" % ( "\n\n>>>" + target, "Admin page found!")
raw_input("Press enter to continue scanning.\n")
else:
var1 = var1
conn.close()
print("\n\nCompleted \n")
print var2, " Admin pages found"
print var1, " total pages scanned"
raw_input("[/] The Game Over; Press Enter to Exit")
#url = raw_input("请输入扫描网站:")
#num = raw_input("请设置线程数:")
url = "www.baidu.com"
num = 100
t=time.time()
print 'Starting scan path on:',url
tp=ThreadPool(url,num)
tp.waitfor_complete()
print 'used time:%f' % (time.time()-t)