| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1715 人关注过本帖
标题:python 线程池扫描路径 py菜鸟 求指点怎么解决!
只看楼主 加入收藏
lonelykin
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-5-2
收藏
 问题点数:0 回复次数:1 
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)

搜索更多相关主题的帖子: 扫描 except python 工作 import 
2013-05-02 14:48
令狐神逸
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-6-21
收藏
得分:0 
看完不顶什么心态?不够觉悟吗?火前留名不知吗?强势插了不知吗?
2013-06-27 18:57
快速回复:python 线程池扫描路径 py菜鸟 求指点怎么解决!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028372 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved