aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-06-21 11:55:13 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-06-22 10:53:34 +0200
commitb57db24a2419ee34d0fdab1e797ee8a760f1512f (patch)
tree6c0ac2dcc8288a0773c70480549dbd74d543e586 /examples
parentd189efa29969ce9731a2954c398c8c7eeb5f5dc2 (diff)
Add QHttpServer::route()/afterRequest()
Complements 77e0363f0257caff13e8fe5fbb9cd1e7f948b066. Add the After Request example. Change-Id: I2d2bcd5993933e9ca133e2f451580633130ae5dc Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/httpserver/afterrequest/doc/afterrequest.rst5
-rw-r--r--examples/httpserver/afterrequest/main.py36
2 files changed, 41 insertions, 0 deletions
diff --git a/examples/httpserver/afterrequest/doc/afterrequest.rst b/examples/httpserver/afterrequest/doc/afterrequest.rst
new file mode 100644
index 000000000..0e81a6707
--- /dev/null
+++ b/examples/httpserver/afterrequest/doc/afterrequest.rst
@@ -0,0 +1,5 @@
+HTTP Server After Request Example
+=================================
+
+A Python application that demonstrates the analogous example in C++
+`AfterRequest Example <https://doc.qt.io/qt-6/qthttpserver-afterrequest-example.html>`_
diff --git a/examples/httpserver/afterrequest/main.py b/examples/httpserver/afterrequest/main.py
new file mode 100644
index 000000000..e68c992ec
--- /dev/null
+++ b/examples/httpserver/afterrequest/main.py
@@ -0,0 +1,36 @@
+# Copyright (C) 2020 Mikhail Svetkin <mikhail.svetkin@gmail.com>
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+"""PySide6 port of the /httpserver/afterrequest from from Qt"""
+
+import sys
+
+from PySide6.QtCore import QCoreApplication
+from PySide6.QtNetwork import QHostAddress
+from PySide6.QtHttpServer import QHttpServer
+
+
+def route(request):
+ return "Hello world"
+
+
+def after_request(response, request):
+ response.setHeader(b"Server", b"Super server!")
+
+
+if __name__ == '__main__':
+ app = QCoreApplication(sys.argv)
+ httpServer = QHttpServer()
+ httpServer.route("/", route)
+
+ httpServer.afterRequest(after_request)
+
+ port = httpServer.listen(QHostAddress.Any)
+ if port == 0:
+ print("Server failed to listen on a port.", file=sys.stderr)
+ sys.exit(-1)
+
+ print(f"Running on http://127.0.0.1:{port}/ (Press CTRL+\\ to quit)")
+
+ sys.exit(app.exec())