from multiprocessing import Process
import MySQLdb
import Queue
import threading
from baidu_detect import BaiduDefender

exitFlag = 0
domain_list=list()
domain_list_tem=list()
baidu_result = dict()
record = []

queueLock=threading.Lock()
workQueue= Queue.Queue()
db = MySQLdb.connect(host = '10.245.146.37', port = 3306, user= 'root', passwd = 'platform', db = 'malicious_domains_profile', charset = 'utf8')

cursor = db.cursor()
try:
    sql = 'select domain from baidu_detect'
    cursor.execute(sql)
    domain_list_temp = cursor.fetchall()
    db.close()
    print('fetch OK')
except:
    print('fetch ERROR.')
for domain in domain_list_temp:
    domain_list.append(str(domain)[3:-3])
    
def press_data(domain):
    result = BaiduDefender.detect_domain(domain)
    db = MySQLdb.connect(host = '10.245.146.37', port = 3306, user= 'root', passwd = 'platform', db = 'malicious_domains_profile', charset = 'utf8')
    cursor = db.cursor()
    try:
        sql = 'update baidu_detect set result ="'+result+'" where domain ="'+domain+'"'
        a=cursor.execute(sql)
        print(a)
        print(sql)
        db.commit()
        db.close()
        print('update OK')
    except:
        print('update ERROR.')
    
def process_data(q):
    while not exitFlag:
        queueLock.acquire()
        if not workQueue.empty():
            data = q.get()
            queueLock.release()
            press_data(data)
        else:
            queueLock.release()
        # time.sleep(1)

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

def multithread(host):
    global queueLock,workQueue
    threadList = 5
    queueLock = threading.Lock()
    workQueue = Queue.Queue(len(host))
    threads = []
    for tName in range(threadList):
        #print('ok')
        thread = myThread(workQueue)
        thread.start()
        threads.append(thread)
    queueLock.acquire()
    for word in host:
        workQueue.put(word)
    queueLock.release()
    while not workQueue.empty():
        pass
    exitFlag = 1
    for t in threads:
        t.join()

multithread(domain_list)




        
