I'm making a Python script that sniffs the network traffic for specific packets. For that, I have several threads. (Let's assume that they are needed.)
I would like to stop these threads when CTRL+C is pressed.
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")
How can I achieve that?