townlong
townlong

Reputation: 189

PyQT4 Asynchronous QTcpServer (How to create event loop)

Trying to convert a server written in C++ into Python. The server was written to be Asynchronous/Non Blocking. What works in C++ doesn't seem to want to work for me in Python

I am using PyQT4. I read Python you have to create the event loop or something along those lines any ideas are greatly appreciated

I should mention what seems to not work is that the incomingConnection function in Class Server is never called.

*cheers

import sys
from PyQt4.QtCore import *
from PyQt4.QtNetwork import *


class Client(QObject):
    def __init__(self, parent=None):
        QObject.__init__(self)
        QThreadPool.globalInstance().setMaxThreadCount(15)

    def SetSocket(self, Descriptor):
        self.socket = QTcpSocket(self)
        self.connect(self.socket, SIGNAL("connected()"), SLOT(self.connected()))
        self.connect(self.socket, SIGNAL("disconnected()"), SLOT(self.disconnected()))
        self.connect(self.socket, SIGNAL("readyRead()"), SLOT(self.readyRead()))

        self.socket.setSocketDescriptor(Descriptor)
        print "Client Connected from IP %s" % self.socket.peerAddress().toString()

    def connected(self):
        print "Client Connected Event"

    def disconnected(self):
        print "Client Disconnected"

    def readyRead(self):
        msg = self.socket.readAll()
        print msg


class Server(QObject):
    def __init__(self, parent=None):
        QObject.__init__(self)

    def incomingConnection(self, handle):
        print "incoming"
        self.client = Client(self)
        self.client.SetSocket(handle)

    def StartServer(self):
        self.server = QTcpServer()
        if self.server.listen(QHostAddress("0.0.0.0"), 8888):
            print "Server is awake"    
        else:
            print "Server couldn't wake up"


def main():
    app = QCoreApplication(sys.argv)
    Server().StartServer()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Upvotes: 0

Views: 2179

Answers (1)

andrean
andrean

Reputation: 6796

incomingConnection is not called because QTcpServer's base implementation of the function is called. as incomingConnection is a vitual function, you just have to assign your's to QTcpServer's incomingConnection attribute, like this:

class Server(QObject):
    def __init__(self, parent=None):
        QObject.__init__(self)

    def incomingConnection(self, handle):
        print "incoming"
        self.client = Client(self)
        self.client.SetSocket(handle)

    def StartServer(self):
        self.server = QTcpServer()
        self.server.incomingConnection = self.incomingConnection
        if self.server.listen(QHostAddress("0.0.0.0"), 8888):
            print "Server is awake"    
        else:
            print "Server couldn't wake up"

you can check out PySide's documentation, as it's much more pythonic than PyQt's, currently hosted only here: http://srinikom.github.com/pyside-docs/

Upvotes: 1

Related Questions