libpcap & Tktiner

Christian Wieser chwieser at tk28.oulu.fi
Sun May 5 09:33:37 EDT 2002


Hi, 

tho I have read quite a lot of different solutions for my problem, I still
could not sort it out. Thats why I dare to ask you gurus for some help:

I use pylibpcap-0.3.1 and python-2.2.1. 

Thats what I want to do:
	* Set up the GUI
	* sniff for UDP-packets
	* add new UDP connections to GUI
	* if no packet arrives within a timeout, erase entry

Problems:
	* GUI freezes when no packets arrive
	* Timeout does not work


Thats how I tried to do it:

[snip]

class GuiPart:
    def __init__(self, master, queue_new, queue_delete, endCommand):
        self.queue_new = queue_new
        self.queue_delete = queue_delete

        # Set up the GUI
        self.multiListBox = MultiListbox(master, (('Source', 20),
                                                ('Destination', 20),
                                                ('BLA',4)),
                                         height=20,
                                         bg='white')
        self.multiListBox.pack(expand=Tkinter.YES,fill=Tkinter.BOTH)
        
        console = Tkinter.Button(master, text='Done', command=endCommand)
        console.pack()
        # Add more GUI stuff here

    def processIncoming(self):
        """
        Handle all the messages currently in the queue (if any).
        """

        #Insert new entries
        print "processIncoming"
        while self.queue_new.qsize():
            try:
                msg = self.queue_new.get(0)
                self.multiListBox.insert (Tkinter.END, msg)
            except Queue.Empty:
                pass
            
        #Erease expired entries
        while self.queue_delete.qsize():
            try:
                msg = self.queue_delete.get(0)
                self.multiListBox.delete(msg)
            except Queue.Empty:
                pass

class List_UDP_Streams:
    """
    """
    def __init__(self, master):
        """
        Start the GUI and the asynchronous threads. And initiate modules
        """
        self.master = master

        # Create the queues
        self.queue_new = Queue.Queue()
        self.queue_delete = Queue.Queue()

        # Set up the GUI part
        self.gui = GuiPart(self.master, self.queue_new, self.queue_delete, self.endApplication)

        self.timeout =  2

[snip]
        self.running = 1

    	self.thread_new_connection = threading.Thread(target=self.workerThread_new_connection)
        self.thread_new_connection.start()

    	self.thread_delete_entry = threading.Thread(target=self.workerThread_delete_entry)
        self.thread_delete_entry.start()

        # Start the periodic call in the GUI to check if the queue contains
        # anything
        self.periodicCall()

    def periodicCall(self):
        """
        Check every 100 ms if there is something new in the queue.
        """
        print "periodicCall"
        self.gui.processIncoming()
        if not self.running:
            # This is the brutal stop of the system. You may want to do
            # some cleanup before actually shutting it down.
            print "periodicCall -> Exit"
            import sys
            sys.exit(1)
        self.master.after(100, self.periodicCall)

    def workerThread_new_connection(self):
        while self.running:
            self.p.dispatch(1, self.pcap_callback)

    def pcap_callback(self,plen, pkt): 
        #print 'pcap_callback'
        
[snip]

    def workerThread_delete_entry(self):
        """ checks every second for timed out connections and ereases them form
        """
        
        while self.running:
            print "timeout(self) called"
	    [if packet in list and timed out]
                self.queue_delete_entry.put(i)
            time.sleep(1000)

    def endApplication(self):
        self.running = 0
        self.master.quit()
        
root = Tkinter.Tk()
print root
client = List_UDP_Streams(root)
root.mainloop()


Thanks for helping me out,

-- 

Christian Wieser
mailto:chwieser at ee.oulu.fi

"Out of the crooked timber of humanity, 
no straight thing was ever made" - Immanuel Kant
 



More information about the Python-list mailing list