簡體   English   中英

在這種情況下,我該如何寫queue.put

[英]In this case, how can I write the queue.put

我正在編寫程序以在同一服務器中獲取域,並且它還可以掃描Web目錄。

#!/usr/bin/env python
#encoding = utf-8
import threading
import urllib,urllib2,httplib
from urllib2 import Request, urlopen, URLError

import Queue,sys
import re
concurrent = 5
url = sys.argv[1]

class Scanner(threading.Thread):


    def __init__(self, work_q):
        threading.Thread.__init__(self)
        self.work_q = work_q


    def getdomains(self):
        doreq = Request('http://www.logontube.com/website/'+ url)
        response = urlopen(doreq)
        html = response.read()
        response.close()
        domains = re.findall('<br><a href=\"(.*?)\" target=\"_blank\"',html)
        return domains

    def run(self):
        alldomains = self.getdomains()
        pathline = [line.rstrip() for line in open("path.txt")]


        while True:
            for aim in alldomains:
                for path in pathline:
                    path = self.work_q.get()

                    req = Request(aim+path)
                    try:
                        response = urlopen(req)
                    except URLError, e:
                        if hasattr(e, 'reason'):
                            print aim+path,'Not Found'
                        elif hasattr(e,'code'):
                            print aim+path,'Not Found'
                    else:
                        try:
                            logs = open('log.txt',"a+")
                        except(IOError):
                            print "[x] Failed to create log file"
                        print aim+path,"Found"
                        logs.writelines(aim+path+"\n")
                        logs.close()

def main():


    work_q = Queue.Queue()
    paths = [line.rstrip() for line in open("path.txt")]
    for i in range(concurrent):
        t = Scanner(work_q)
        t.setDaemon(True)
        t.start()

    for path in paths:
        work_q.put(path)

    work_q.join()


main()         

問題是該程序僅執行路徑循環,因此我只能獲取一個網站的掃描結果。 我發現了問題,

for path in paths:
        work_q.put(path) # The program finishes when it puts all the path

如果您想幫助我測試該程序,則可能需要一些網站目錄(另存為path.txt)。

/default.asp
/index.asp
/index.htm
/index.html
/index.jsp
/index.php
/admin.asp
/admin.php
/admin.shtml
/admin.txt
/admin_admin.asp
/config.asp
/inc/
/login.asp
/login.jsp
/login.php
/login/
/phpinfo.php
/readme.txt
/robots.txt
/test.asp
/test.html
/test.txt
/test.php
/news/readme.txt
/addmember/

你需要一個:

while 1:
   pass

或等到線程完成后退出的內容。

發生的事情是,您正在啟動線程,但是正在終止主線程,因此您永遠無法看到線程的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM