'Terminate a python thread which sends packets using scapy
I am creating an application based on Scapy. Basically, it allows the user to send packets on a network interface by choosing the interface, passing some options (the packet, loop, sending interval, packet count) and then starting a thread that sends the packets on that interface (using sendp()). During the packet transmission, the main thread continues execution. Since it uses threads, the user is free to start as many threads as he wants (maybe to send packets on multiple interfaces at the same time).
However, if the user sets loop=True to send packets for an indefinite amount of time, this means sendp() will keep sending packets. I need to find a way to stop the execution of that function, or to terminate the entire thread. All examples of 'killing' threads that I found use a loop within the run() function to periodically check a thread stop condition. But a loop is no use for me since sendp() blocks that thread indefinitely.
I thought that maybe there is a way to send a KeyboardInterrupt from the main thread to the child send() thread that would stop the execution of sendp(), but I found no way to do this.
Could you help me out with some suggestions on how can I stop the function/thread from running??
Solution 1:[1]
So, I couldn't find a better solution and I settled for Event() and loops. Basically, if the user sets loop=True, the function called in the spawned thread will be sendp(count=500) within a while loop. After each 500 packets sent, a stop condition will be checked. If it is False, sendp() will be called again for another 500 packets. If it is True, after finishing to send the 500 packets, sendp() won't be called again and the thread will stop. The stop condition is set by the user from input().
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cosmin Ionut Grosu |
