Question

I am trying to empty the list whenever there is a keyboard interrupt but this doesn't work. Below is my code.

while 1:
    try:
       n=0
       while n<10:
           pkt=sniff(prn=lambda x:x.sprintf("{Ether : %ether.src% --> %ether.dst%}"),timeout=1,store=1)
           buffpkt+=pkt[:]  ##Store packets in a list buffer
           n+=1
       self._tempbuffstore(buffpkt)

    except KeyboardInterrupt:
        buffpkt=[]  ##Flush the list asynchronously "THIS IS THE MAIN REQUIREMENT FOR INTERRUPT"
        raise

I tried doing all possible things but this interrupt handler is not working. Any help?

Was it helpful?

Solution

It's looks like you call to 'sniff', which I can't reproduce, is blocking. It's very common for network-related methods.

You should put your sniff method in a separate thread, and then, it's possible to handle your exception in the main loop (and you'll also have to deal with stopping the thread on exceptions).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top