Sorry to bother again:) but im new to Python and to OOP.
I made a simple sniffer application (thanks to your help) which prints the sniffed data on the console. I want to extend it giving it a GUI using PyQt4. I also made an argument "--nogui" that the user can give to stay just on the console.

So I made the method def gui_callback(self, data) on the sniffer thread to pass the data on the gui class' "window" object, where the method def callbackSetText(self, data) has to be called somehow.

What am i supposed to put on the def gui_callback(self, data):??

test.py

import sys
from threading import Thread
from scapy.all import sniff
from PyQt4 import QtCore, QtGui
from testgui_ui import Ui_GuiForm

class SnifferThread(Thread):
    def __init__ (self):
        Thread.__init__(self)
        self.filter = "tcp port 80"
        self.timeToStop = False
        self.havegui = False

    def HaveGui(self):
        self.havegui = True

    def run(self):
        print "Sniffer started..."
        sniff(filter=self.filter, prn=self.pkt_callback, stopperTimeout=0.1, stopper=self.timeToStopChecker, store=0)
        
    def stop(self):
        print "Sniffer stoping..."
        self.timeToStop = True
        
    def timeToStopChecker(self):
        return self.timeToStop

    def gui_callback(self, data):
        print "Sending data to GUI..."
        #What am i supposed to put in here??
    
    def pkt_callback(self,pkt):
        data = pkt.sprintf('%IP.src% > %IP.dst%')
        print data
        if self.havegui : self.gui_callback(data)

class GuiForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_GuiForm()
        self.ui.setupUi(self)
    
    def callbackSetText(self, data):
        #Receiving data from sniffer thread
        #and puting them in a textedit widget
        self.ui.textedit.setText(data)
        
def terminateSnifferThread():
    sniffer.stop()
    sniffer.join()

if __name__ == '__main__':
    sniffer = SnifferThread()
    sniffer.start()
    
    if not "--nogui" in sys.argv[1:]:
        app = QtGui.QApplication([])
        window = GuiForm()
        window.show()
        sniffer.HaveGui()

        app.exec_()
        terminateSnifferThread()

    try:
        while(sniffer.timeToStop != True): pass
    except KeyboardInterrupt:
        terminateSnifferThread()

I think its simple enough. I just want to call a method from one class' object to another one on a different thread.
Here is the ui code if someone want to test it out.

testgui_ui.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'testgui.ui'
#
# Created: Wed Nov 18 00:35:47 2009
#      by: PyQt4 UI code generator 4.6
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_GuiForm(object):
    def setupUi(self, GuiForm):
        GuiForm.setObjectName("GuiForm")
        GuiForm.resize(381, 324)
        self.centralwidget = QtGui.QWidget(GuiForm)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtGui.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.textedit = QtGui.QTextEdit(self.centralwidget)
        self.textedit.setReadOnly(True)
        self.textedit.setObjectName("textedit")
        self.gridLayout.addWidget(self.textedit, 0, 0, 1, 1)
        GuiForm.setCentralWidget(self.centralwidget)

        self.retranslateUi(GuiForm)
        QtCore.QMetaObject.connectSlotsByName(GuiForm)

    def retranslateUi(self, GuiForm):
        GuiForm.setWindowTitle(QtGui.QApplication.translate("GuiForm", "myGUI", None, QtGui.QApplication.UnicodeUTF8))

OK i found a solution. I dont know if this is the best way but it works.
I read this article: Passing python vars in PyQt using SIGNAL/SLOT...
And i managed to do it using signals/slots.. here is the code:

def gui_callback(self, data):
        #Sending data to GUI
        window.emit(QtCore.SIGNAL('packetReceived(PyQt_PyObject)'), data)
    
    def pkt_callback(self,pkt):
        data = pkt.sprintf('%IP.src% > %IP.dst%')
        print data
        if self.havegui : self.gui_callback(data)

class GuiForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_GuiForm()
        self.ui.setupUi(self)
        self.connect(self, QtCore.SIGNAL('packetReceived(PyQt_PyObject)'), self.callbackSetText)
    
    def callbackSetText(self, data):
        #Receiving data from sniffer thread
        #and puting them in a textedit widget
        self.ui.textedit.append(data)
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.