0

我使用 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”,我希望程序退出。我能知道我在这里做错了什么吗?

4

1 回答 1

1

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  
于 2013-05-11T01:43:21.957 回答