2013-11-25 29 views
0

我一直試圖找到一種方法來殺死那些在這一程序結束無響應的線程:我怎麼殺反應遲鈍的線程

大部分的代碼工作的時間,但在某些特定領域線程將掛起,不允許程序完成。

任何幫助將不勝感激。

#!/usr/bin/python 

from socket import gethostbyaddr 
import dns.resolver 
import sys 
import Queue 
import threading 
import subprocess 
import time 

exitFlag = 0 
lines = '' 

class myThread (threading.Thread): 
    def __init__(self, threadID, name, q): 
     threading.Thread.__init__(self) 
     self.threadID = threadID 
     self.name = name 
     self.q = q 
    def run(self): 
     process_data(self.name, self.q) 

class Timer(): 
    def __enter__(self): self.start = time.time() 
    def __exit__(self, *args): 
     taken = time.time() - self.start 
     print " [*] Time elapsed " + str(round(taken,1)) + " seconds at " + str(round(len(subdomains)/taken)) + " lookups per second." 

def process_data(threadName, q): 
    while not exitFlag: 
     queueLock.acquire() 
     if not workQueue.empty(): 
      data = q.get() 
      queueLock.release() 
      host = data.strip() + '.' + domain.strip() 
      try: 
       answers = resolver.query(host) 
       try: 
        output = gethostbyaddr(host) 
        if len(host) < 16: 
         print str(host) + "\t\t" + str(output[0]) + " " + str(output[2]) 
         found.append(str(host) + "\t\t" + str(output[0]) + " " + str(output[2])) 
        else: 
         print str(host) + "\t" + str(output[0]) + " " + str(output[2]) 
         found.append(str(host) + "\t" + str(output[0]) + " " + str(output[2])) 
       except: 
        print str(host) 
        found.append(str(host)) 
      except: 
       pass 
     else: 
      queueLock.release() 

if len(sys.argv) < 3: 
    print 
    print 'Usage: dnsbrute.py <target.com> <subdomains.txt> (threads)' 
    exit() 

if len(sys.argv) >= 4: 
    maxthreads = int(sys.argv[3]) 
else: 
    maxthreads = int(40) 

domain = sys.argv[1] 
maked = "mkdir -p logs" 
process = subprocess.Popen(maked.split(), stdout=subprocess.PIPE) 
poutput = process.communicate()[0] 
found = [] 
subdomains = [line.strip() for line in open(sys.argv[2], 'r')] 
dnsservers = ["8.8.8.8", "8.8.4.4", "4.2.2.1", "4.2.2.2", "4.2.2.3", "4.2.2.4", "4.2.2.5", "4.2.2.6", "209.244.0.3", "209.244.0.4" ] 
threadList = [] 
numthreads = 1 
resolver = dns.resolver.Resolver() 
resolver.nameservers = dnsservers 
logfile = open("logs/" + domain + ".log", 'w') 

while numthreads <= maxthreads: 
    threadList.append(str("Thread-") + str(numthreads)) 
    numthreads += 1 

print " [*] Starting " + str(maxthreads) + " threads to process " + str(len(subdomains)) + " subdomains." 
print 

queueLock = threading.Lock() 
workQueue = Queue.Queue(len(subdomains)) 
threads = [] 
threadID = 1 

with Timer(): 
    for tName in threadList: 
     thread = myThread(threadID, tName, workQueue) 
     thread.start() 
     threads.append(thread) 
     threadID += 1 

    queueLock.acquire() 
    for work in subdomains: 
     workQueue.put(work) 
    queueLock.release() 

    while not workQueue.empty(): 
     pass 

    exitFlag = 1 

    for t in threads: 
     t.join() 

    for item in found: 
     logfile.write("%s\n" % item) 

    print 
    print " [*] All threads complete, " + str(len(found)) + " subdomains found." 
    print " [*] Results saved to logs/" + domain + ".log" 

回答

0

請記住,以任何語言殺死線程是一種糟糕的方法,因爲資源可能會處於不一致的狀態。如果可以的話,嘗試重新設計你的程序,使線程通過檢查一個布爾值來關閉它們自己。無論如何,這是一個非常好的答覆從這裏的同胞SO:

Is there any way to kill a Thread in Python?

相關問題