aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2024-09-09 08:16:28 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2024-09-09 10:50:42 +0200
commit8466bbd0b96f98d2c933f60c05afab7913c6a918 (patch)
tree1e176e9dfe0a6a27f7e402929f76410e7dd65984 /examples
parent8e7ec5dad905f3a0ad6770d0fc6b3c48cb0c74fa (diff)
QtHttpServer: Fix example
Rename the example, adapt to 6.8 and add a pyproject file. Make it clear that it is a simplified version. Task-number: QTBUG-128113 Task-number: PYSIDE-2620 Change-Id: If6cb578f3f0c7405041404f90e014dcfb3903e03 Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io>
Diffstat (limited to 'examples')
-rw-r--r--examples/httpserver/afterrequest/doc/afterrequest.rst5
-rw-r--r--examples/httpserver/simplehttpserver/doc/simplehttpserver.rst5
-rw-r--r--examples/httpserver/simplehttpserver/main.py (renamed from examples/httpserver/afterrequest/main.py)16
-rw-r--r--examples/httpserver/simplehttpserver/simplehttpserver.pyproject3
4 files changed, 18 insertions, 11 deletions
diff --git a/examples/httpserver/afterrequest/doc/afterrequest.rst b/examples/httpserver/afterrequest/doc/afterrequest.rst
deleted file mode 100644
index 0e81a6707..000000000
--- a/examples/httpserver/afterrequest/doc/afterrequest.rst
+++ /dev/null
@@ -1,5 +0,0 @@
-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/simplehttpserver/doc/simplehttpserver.rst b/examples/httpserver/simplehttpserver/doc/simplehttpserver.rst
new file mode 100644
index 000000000..f23998eb3
--- /dev/null
+++ b/examples/httpserver/simplehttpserver/doc/simplehttpserver.rst
@@ -0,0 +1,5 @@
+Simple HTTP Server Example
+==========================
+
+A simplified version of the C++ example
+`Simple HTTP Server Example <https://doc.qt.io/qt-6/qthttpserver-simple-example.html>`_
diff --git a/examples/httpserver/afterrequest/main.py b/examples/httpserver/simplehttpserver/main.py
index 7971a1c3a..eb3f56948 100644
--- a/examples/httpserver/afterrequest/main.py
+++ b/examples/httpserver/simplehttpserver/main.py
@@ -8,7 +8,7 @@ from __future__ import annotations
import sys
from PySide6.QtCore import QCoreApplication
-from PySide6.QtNetwork import QHostAddress
+from PySide6.QtNetwork import QHttpHeaders, QTcpServer
from PySide6.QtHttpServer import QHttpServer
@@ -16,8 +16,11 @@ def route(request):
return "Hello world"
-def after_request(response, request):
- response.setHeader(b"Server", b"Super server!")
+def after_request(request, response):
+ headers = response.headers()
+ headers.append(QHttpHeaders.WellKnownHeader.WWWAuthenticate,
+ 'Basic realm="Simple example", charset="UTF-8"')
+ response.setHeaders(headers)
if __name__ == '__main__':
@@ -25,12 +28,13 @@ if __name__ == '__main__':
httpServer = QHttpServer()
httpServer.route("/", route)
- httpServer.afterRequest(after_request)
+ httpServer.addAfterRequestHandler(httpServer, after_request)
- port = httpServer.listen(QHostAddress.Any)
- if port == 0:
+ tcpServer = QTcpServer()
+ if not tcpServer.listen() or not httpServer.bind(tcpServer):
print("Server failed to listen on a port.", file=sys.stderr)
sys.exit(-1)
+ port = tcpServer.serverPort()
print(f"Running on http://127.0.0.1:{port}/ (Press CTRL+\\ to quit)")
diff --git a/examples/httpserver/simplehttpserver/simplehttpserver.pyproject b/examples/httpserver/simplehttpserver/simplehttpserver.pyproject
new file mode 100644
index 000000000..cc7a74a34
--- /dev/null
+++ b/examples/httpserver/simplehttpserver/simplehttpserver.pyproject
@@ -0,0 +1,3 @@
+{
+ "files": ["main.py"]
+}