簡體   English   中英

python多線程嗅探器-從線程退出

[英]python multithreaded sniffer- exit from thread

我已經使用scapy在python中創建了一個數據包嗅探器,但是卡在了多線程的東西中。

def sniffer(ip):
    filter_str = "icmp and host " + ip
    packets=sniff(filter=filter_str,count=20)
    status= False
    for p in packets:
        packet_load=str(p['Raw'].load)
        if packet_load.find("@@")!= -1:
                status=True
                log_thread = Thread(target=logger,args=(packets,))
                log_thread.start()
                log_thread.join()
                break
    if status==True:
        print "Suspicious Packets sniffed!!"

    user_ip = raw_input("Do you want to continue sniffing???(y/n)")
    while 1:
        if user_ip=="y" or user_ip=="Y":
            new_thread = Thread(target=sniffer, args=(ip,))
            new_thread.start()
            new_thread.join()
        else:
            #need somthing to quit the program
   return  

在這里,我的嗅探器一次嗅探20個數據包,並等待用戶輸入進一步嗅探。 但是,如果用戶輸入“ n”作為輸入,則程序將掛起。 理想情況下,如果用戶輸入“ n”,我希望程序退出。 我能知道我在做什么錯嗎?

寫有限循環時while 1很少是一個好選擇。 嘗試使用標志代替:

leaving = False
while not leaving:

    user_ip = raw_input("Do you want to continue sniffing???(y/n)")
    if user_ip.lower() == 'y':
        new_thread = Thread(target=sniffer, args=(ip,))
        new_thread.start()
        new_thread.join()
    elif user_ip.lower() == 'n':
        print "Leaving sniffer"
        leaving = True
return  

暫無
暫無

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

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