我正在制作一个Python脚本来嗅探特定数据包的网络流量。为此,我有几个主题。 (我们假设他们是必需的。)
我想在按下CTRL + C时停止这些线程。
import threading
from scapy.* import all
class myThread(threading.Thread):
def __init__(self, protocol):
threading.Thread.__init__(self)
self.protocol = protocol
def run(self):
my_function(self.protocol)
def callback(packet):
# Do stuff
def my_function(protocol):
# Do stuff
sniff(filter=protocol, prn=callback, store=0)
t1 = myThread("udp")
t2 = myThread("tcp")
t1.start()
t2.start()
# magic stuff so that CTRL+C stops the threads
t1.join()
t2.join()
print("Exiting")
我怎样才能做到这一点?