How do I kill unresponsive threads
Question: I've been trying to find a way to kill the threads that are unresponsive at
the end of this program:

Most of the time the code works, but on certain domains some of the threads
will hang, not allowing the program to complete.

Any help would be much appreciated.

    
    
        #!/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"
    


Answer: Please bear in mind that it is a bad approach to kill threads in whatever
language, becuase the resources can be left in an unconsistent state. If you
can, try to redesign your program such that the threads will close themselves
by checking a boolean value. Anyway, a very good answer from a fellow here on
SO:

[Is there any way to kill a Thread in
Python?](http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-
thread-in-python)

