1#!/usr/bin/env python
2
3
4#############################################################################
5##
6## Copyright (C) 2013 Riverbank Computing Limited
7## Copyright (C) 2010 Hans-Peter Jansen <hpj@urpla.net>.
8## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
9## All rights reserved.
10##
11## This file is part of the examples of PyQt.
12##
13## $QT_BEGIN_LICENSE:LGPL$
14## Commercial Usage
15## Licensees holding valid Qt Commercial licenses may use this file in
16## accordance with the Qt Commercial License Agreement provided with the
17## Software or, alternatively, in accordance with the terms contained in
18## a written agreement between you and Nokia.
19##
20## GNU Lesser General Public License Usage
21## Alternatively, this file may be used under the terms of the GNU Lesser
22## General Public License version 2.1 as published by the Free Software
23## Foundation and appearing in the file LICENSE.LGPL included in the
24## packaging of this file.  Please review the following information to
25## ensure the GNU Lesser General Public License version 2.1 requirements
26## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
27##
28## In addition, as a special exception, Nokia gives you certain additional
29## rights.  These rights are described in the Nokia Qt LGPL Exception
30## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
31##
32## GNU General Public License Usage
33## Alternatively, this file may be used under the terms of the GNU
34## General Public License version 3.0 as published by the Free Software
35## Foundation and appearing in the file LICENSE.GPL included in the
36## packaging of this file.  Please review the following information to
37## ensure the GNU General Public License version 3.0 requirements will be
38## met: http://www.gnu.org/copyleft/gpl.html.
39##
40## If you have questions regarding the use of this file, please contact
41## Nokia at qt-info@nokia.com.
42## $QT_END_LICENSE$
43##
44#############################################################################
45
46
47import random
48
49from PyQt5.QtCore import QByteArray, QDataStream, QIODevice
50from PyQt5.QtWidgets import (QApplication, QDialog, QLabel, QHBoxLayout,
51        QMessageBox, QPushButton, QVBoxLayout)
52from PyQt5.QtNetwork import QLocalServer
53
54
55class Server(QDialog):
56    def __init__(self, parent=None):
57        super(Server, self).__init__(parent)
58
59        statusLabel = QLabel()
60        statusLabel.setWordWrap(True)
61        quitButton = QPushButton("Quit")
62        quitButton.setAutoDefault(False)
63
64        self.fortunes = (
65            "You've been leading a dog's life. Stay off the furniture.",
66            "You've got to think about tomorrow.",
67            "You will be surprised by a loud noise.",
68            "You will feel hungry again in another hour.",
69            "You might have mail.",
70            "You cannot kill time without injuring eternity.",
71            "Computers are not intelligent. They only think they are.",
72        )
73
74        self.server = QLocalServer()
75        if not self.server.listen('fortune'):
76            QMessageBox.critical(self, "Fortune Server",
77                    "Unable to start the server: %s." % self.server.errorString())
78            self.close()
79            return
80
81        statusLabel.setText("The server is running.\nRun the Fortune Client "
82                "example now.")
83
84        quitButton.clicked.connect(self.close)
85        self.server.newConnection.connect(self.sendFortune)
86
87        buttonLayout = QHBoxLayout()
88        buttonLayout.addStretch(1)
89        buttonLayout.addWidget(quitButton)
90        buttonLayout.addStretch(1)
91
92        mainLayout = QVBoxLayout()
93        mainLayout.addWidget(statusLabel)
94        mainLayout.addLayout(buttonLayout)
95        self.setLayout(mainLayout)
96
97        self.setWindowTitle("Fortune Server")
98
99    def sendFortune(self):
100        block = QByteArray()
101        out = QDataStream(block, QIODevice.WriteOnly)
102        out.setVersion(QDataStream.Qt_4_0)
103        out.writeUInt16(0)
104        out.writeQString(random.choice(self.fortunes))
105        out.device().seek(0)
106        out.writeUInt16(block.size() - 2)
107
108        clientConnection = self.server.nextPendingConnection()
109        clientConnection.disconnected.connect(clientConnection.deleteLater)
110        clientConnection.write(block)
111        clientConnection.flush()
112        clientConnection.disconnectFromServer()
113
114
115if __name__ == '__main__':
116
117    import sys
118
119    app = QApplication(sys.argv)
120    server = Server()
121    server.show()
122    sys.exit(app.exec_())
123