allfro/pcappy

Interrupt loop() after open_live()

alexvoronov opened this issue · 1 comments

Hi,

Is it possible to break out of loop()? When I press Ctrl+C, I see KeyboarInterrupt printed in the terminal, but the program continues, so I have to close the terminal tab to terminate. Is there any other way?

Below is the example I'm running:

import pcappy
from sys import argv

p = pcappy.open_live(argv[1])

def gotpacket(d, hdr, data):
    print d, hdr, repr(data)
    d['count'] += 1

d = {'count': 0}

p.loop(-1, gotpacket, d)      

I found solution here: http://stackoverflow.com/questions/14271697/ctrlc-doesnt-interrupt-call-to-shared-library-using-ctypes-in-python

The last line in the example above (p.loop(-1, gotpacket, d)) is replaced by:

import threading

t = threading.Thread(target=p.loop, args=[-1, gotpacket, d])
t.daemon = True
t.start()
while t.is_alive():  # wait for the thread to exit
    t.join(.1)