PySide6.QtHttpServer.QHttpServer

class QHttpServer

QHttpServer is a simplified API for QAbstractHttpServer and QHttpServerRouter . More

Inheritance diagram of PySide6.QtHttpServer.QHttpServer

Synopsis

Methods

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description

QHttpServer allows to create a simple Http server by setting a range of request handlers.

The route function can be used to conveniently add rules to the servers QHttpServerRouter . To register a handler to be called after every request use addAfterRequestHandler and to register a handler for all unhandled requests use setMissingHandler .

Minimal example:

QHttpServer server;

server.route("/", [] () {
    return "hello world";
});

auto tcpserver = new QTcpServer();
if (!tcpserver->listen() || !server.bind(tcpserver.get())) {
    delete tcpserver;
    return -1;
}
qDebug() << "Listening on port" << tcpserver->serverPort();
__init__([parent=None])
Parameters:

parentQObject

Creates an instance of QHttpServer with parent parent.

addAfterRequestHandler(context, callback)
Parameters:
  • contextQObject

  • callbackPyCallable

clearMissingHandler()

Resets the handler to the default one that produces replies with status 404 Not Found.

route(rule, callback)
Parameters:
  • rule – str

  • callbackPyCallable

Return type:

bool

router()
Return type:

QHttpServerRouter

Returns a pointer to the constant router object.