PySide6.QtHttpServer.QHttpServer

class QHttpServer

QHttpServer is a simplified API for QAbstractHttpServer and QHttpServerRouter .

Details

QHttpServer is used to create a simple HTTP server by registering a range of request handlers.

The route function can be used to conveniently add rules to the server’s QHttpServerRouter . To register a handler that is called after every request to further process the response use addAfterRequestHandler , but this mechanism only works for routes returning QHttpServerResponse or QFuture<QHttpServerResponse>. 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)) {
    delete tcpserver;
    return -1;
}
qDebug() << "Listening on port" << tcpserver->serverPort();

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

__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.

See also

setMissingHandler

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

  • callbackPyCallable

Return type:

bool

router()
Return type:

QHttpServerRouter

Returns a pointer to the constant router object.