aboutsummaryrefslogtreecommitdiffstats
path: root/examples/network
diff options
context:
space:
mode:
authorMatti Airas <matti.p.airas@nokia.com>2010-09-07 15:11:50 +0300
committerMatti Airas <matti.p.airas@nokia.com>2010-09-10 23:01:39 +0300
commit26aa329f4d69cb2db54101f31f89310586b933a9 (patch)
tree4cfe7602ea8b139e8143c70bf5ea128bb1a61abd /examples/network
parent0945626e9dea923cd755af984a06af21e3d6c75a (diff)
merged threadedfortuneserver.py from PyQt
Diffstat (limited to 'examples/network')
-rwxr-xr-xexamples/network/threadedfortuneserver.py93
1 files changed, 52 insertions, 41 deletions
diff --git a/examples/network/threadedfortuneserver.py b/examples/network/threadedfortuneserver.py
index 24f6935..0cebab8 100755
--- a/examples/network/threadedfortuneserver.py
+++ b/examples/network/threadedfortuneserver.py
@@ -23,24 +23,26 @@
#
############################################################################
-import sys
import random
+
from PySide import QtCore, QtGui, QtNetwork
class FortuneThread(QtCore.QThread):
+ error = QtCore.Signal(QtNetwork.QTcpSocket.SocketError)
+
def __init__(self, socketDescriptor, fortune, parent):
- QtCore.QThread.__init__(self, parent)
+ super(FortuneThread, self).__init__(parent)
self.socketDescriptor = socketDescriptor
self.text = fortune
-
+
def run(self):
tcpSocket = QtNetwork.QTcpSocket()
if not tcpSocket.setSocketDescriptor(self.socketDescriptor):
- self.emit(QtCore.SIGNAL("error(int)"), tcpSocket.error())
+ self.error.emit(tcpSocket.error())
return
-
+
block = QtCore.QByteArray()
outstr = QtCore.QDataStream(block, QtCore.QIODevice.WriteOnly)
outstr.setVersion(QtCore.QDataStream.Qt_4_0)
@@ -48,69 +50,78 @@ class FortuneThread(QtCore.QThread):
outstr.writeString(self.text)
outstr.device().seek(0)
outstr.writeUInt16(block.count() - 2)
-
+
tcpSocket.write(block)
tcpSocket.disconnectFromHost()
tcpSocket.waitForDisconnected()
-
+
class FortuneServer(QtNetwork.QTcpServer):
def __init__(self, parent=None):
- QtNetwork.QTcpServer.__init__(self, parent)
-
- self.fortunes = [
- self.tr("You've been leading a dog's life. Stay off the furniture."),
- self.tr("You've got to think about tomorrow."),
- self.tr("You will be surprised by a loud noise."),
- self.tr("You will feel hungry again in another hour."),
- self.tr("You might have mail."),
- self.tr("You cannot kill time without injuring eternity."),
- self.tr("Computers are not intelligent. They only think they are."),
- ]
+ super(FortuneServer, self).__init__(parent)
+
+ self.fortunes = (
+ "You've been leading a dog's life. Stay off the furniture.",
+ "You've got to think about tomorrow.",
+ "You will be surprised by a loud noise.",
+ "You will feel hungry again in another hour.",
+ "You might have mail.",
+ "You cannot kill time without injuring eternity.",
+ "Computers are not intelligent. They only think they are.")
def incomingConnection(self, socketDescriptor):
- fortune = self.fortunes[random.randint(0, len(self.fortunes)-1)]
+ fortune = self.fortunes[random.randint(0, len(self.fortunes) - 1)]
+
+ try:
+ # Python v3.
+ fortune = bytes(fortune, encoding='ascii')
+ except:
+ # Python v2.
+ pass
+
thread = FortuneThread(socketDescriptor, fortune, self)
- self.connect(thread, QtCore.SIGNAL("finished()"), thread, QtCore.SLOT("deleteLater()"))
+ thread.finished.connect(thread.deleteLater)
thread.start()
class Dialog(QtGui.QDialog):
def __init__(self, parent=None):
- QtGui.QDialog.__init__(self, parent)
-
+ super(Dialog, self).__init__(parent)
+
self.server = FortuneServer()
-
- self.statusLabel = QtGui.QLabel()
- self.quitButton = QtGui.QPushButton(self.tr("Quit"))
- self.quitButton.setAutoDefault(False)
-
+
+ statusLabel = QtGui.QLabel()
+ quitButton = QtGui.QPushButton("Quit")
+ quitButton.setAutoDefault(False)
+
if not self.server.listen():
- QtGui.QMessageBox.critical(self, self.tr("Threaded Fortune Server"),
- self.tr("Unable to start the server: %(error)s.")
- % {'error': self.server.errorString()})
+ QtGui.QMessageBox.critical(self, "Threaded Fortune Server",
+ "Unable to start the server: %s." % self.server.errorString())
self.close()
return
-
- self.statusLabel.setText(self.tr("The server is running on port %(port)d.\n"
- "Run the Fortune Client example now.")
- % {'port': self.server.serverPort()})
- self.connect(self.quitButton, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("close()"))
-
+ statusLabel.setText("The server is running on port %d.\nRun the "
+ "Fortune Client example now." % self.server.serverPort())
+
+ quitButton.clicked.connect(self.close)
+
buttonLayout = QtGui.QHBoxLayout()
buttonLayout.addStretch(1)
- buttonLayout.addWidget(self.quitButton)
-
+ buttonLayout.addWidget(quitButton)
+ buttonLayout.addStretch(1)
+
mainLayout = QtGui.QVBoxLayout()
- mainLayout.addWidget(self.statusLabel)
+ mainLayout.addWidget(statusLabel)
mainLayout.addLayout(buttonLayout)
self.setLayout(mainLayout)
- self.setWindowTitle(self.tr("Threaded Fortune Server"))
+ self.setWindowTitle("Threaded Fortune Server")
+
+
+if __name__ == '__main__':
+ import sys
-if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
dialog = Dialog()
dialog.show()