He estado peleando un buen par de horas con intentar hacer un servidor y cliente TCP de ejemplo para pyqt, no he encontrado ninguna documentación interesante, así que lo cuelgo aquí por si a alguien lo esta buscando, o si algún dia me hace falta.

Este es el codigo de server.py

from PyQt4 import QtCore, QtNetwork
import sys, time

class Server(QtNetwork.QTcpServer):
        def __init__(self, port):
                QtNetwork.QTcpServer.__init__(self)
                addr = QtNetwork.QHostAddress(QtNetwork.QHostAddress.Any)
                while not self.listen(addr, port):
                        print "[Server] Couldn't bind to port, waiting 5 seconds and retrying"
                        time.sleep(5)
                print "[Server] Listening on port", port
                self.setMaxPendingConnections (100)
        def incomingConnection(self, x):
                QtNetwork.QTcpServer.incomingConnection(self, x)
                socket = self.nextPendingConnection()
                socket.connect(socket, QtCore.SIGNAL("readyRead()"), lambda: self.readyRead(socket))
                socket.connect(socket, QtCore.SIGNAL("disconnected()"), lambda: self.disconnected(socket))
                print "[incomingConnection] state is %d" % socket.state()
        def readyRead(self, socket):
                while socket.canReadLine():
                        linea = str(socket.readLine()).strip()
                        print "[readyRead] line: '%s' from socket %s" % (linea, socket)
        def disconnected(self, socket):
                print "[disconnected] state is %d" % socket.state()

app = QtCore.QCoreApplication(sys.argv)
server = Server(23105)
sys.exit(app.exec_())

Y el siguiente es el codigo del client.py

from PyQt4 import QtCore, QtNetwork
import sys, time

class Client(QtNetwork.QTcpSocket):
        def __init__(self, addr, port):
                QtNetwork.QTcpSocket.__init__(self)
                self.connect(self, QtCore.SIGNAL("connected()"), self.connected)
                self.connect(self, QtCore.SIGNAL("disconnected()"), self.disconnected)
                self.connect(self, QtCore.SIGNAL("readyRead()"), self.readyRead)
                #addr = QtNetwork.QHostAddress(QtNetwork.QHostAddress.LocalHost)
                addr = QtNetwork.QHostAddress(addr)
                port = port
                self.connectToHost(addr, port)
                while not self.waitForConnected():
                        print "[Client] Couldn't connecto to host, waiting 5 seconds and retrying"
                        time.sleep(5)
                        self.connectToHost(addr, port)
        def connected(self):
                print "[connected] state is %d" % self.state()
        def readyRead(self):
                while self.canReadLine():
                        linea = str(self.readLine()).strip()
                        print "[readyRead] line '%s' from socket %s" % (linea, self)
        def disconnected(self):
                print "[disconnected] state is %d" % self.state()

app = QtCore.QCoreApplication(sys.argv)
c = Client("127.0.0.1", 23105)
sys.exit(app.exec_())
Anuncios