문제

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?

도움이 되었습니까?

해결책

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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top