How do i stop the following thread?

from threading import Thread
from scapy.all import sniff

class SnifferThread(Thread):
    def __init__ (self,filter):
        Thread.__init__(self)
        self.filter = filter

    def run(self):
        sniff(filter=self.filter, prn=self.pkt_callback, store=0)

    def pkt_callback(self,pkt):
        print pkt.sprintf('%TCP.payload%')

if __name__ == '__main__':
    sniffer = SnifferThread("tcp port 80")
    sniffer.start()
    #here i will make a GUI using PyQt4

Should i kill it somehow? If i do kill it will the memory be cleaned?

Recommended Answers

All 5 Replies

Isn't it the join() method of the thread?

How do i stop the following thread?

from threading import Thread
from scapy.all import sniff

class SnifferThread(Thread):
    def __init__ (self,filter):
        Thread.__init__(self)
        self.filter = filter

    def run(self):
        sniff(filter=self.filter, prn=self.pkt_callback, store=0)

    def pkt_callback(self,pkt):
        print pkt.sprintf('%TCP.payload%')

if __name__ == '__main__':
    sniffer = SnifferThread("tcp port 80")
    sniffer.start()
    #here i will make a GUI using PyQt4

Should i kill it somehow? If i do kill it will the memory be cleaned?

Yes. sniffer.join() should solve your problem.

No it doesn't work.:(
The problem is with the sniff(). I don't know if im doing this right.
It runs forever..
When i do the following i can stop it with KeyboardInterrupt:

from scapy.all import sniff

def pkt_callback(pkt):
    print pkt.sprintf('%TCP.payload%')

if __name__ == '__main__':
    try:
        sniff(filter="tcp port 80", prn=pkt_callback, store=0)
    except KeyboardInterrupt:
        exit(0)

But when it runs from a class it doesnt catch the KeyboardInterrupt.

wow:) That seems to be the solution!
Thank you all three of you!
@ Gribouillis : i'm checking it right now.. if it works i'll mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.