PySide6.QtHttpServer.QHttpServerRouter¶
- class QHttpServerRouter¶
Provides functions to bind a path to a
ViewHandler.Details
QHttpServerRouteris a rule-based system for routing HTTP requests to their appropriate handlers. You can addQHttpServerRouterRuleinstances, which define a request path and its corresponding handler.Variable parts in the route can be specified with placeholders (
"<arg>") in the request path, but it is not needed at the end. The handler receives the matched values as a QRegularExpressionMatch. The arguments can be of any type for which aconverteris available. The handler creation can be simplified withbindCaptured().Note
A
QHttpServerRouterinstance must not be modifed by its rules.Note
This is a low-level routing API for an HTTP server.
Minimal example:
auto pageView = [] (const quint64 page) { qDebug() << "page" << page; }; using ViewHandler = decltype(pageView); QHttpServerRouter router; // register callback pageView on request "/page/<number>" // for example: "/page/10", "/page/15" router.addRule<ViewHandler>( new QHttpServerRouterRule("/page/", [=] (QRegularExpressionMatch &match, const QHttpServerRequest &, QHttpServerResponder &&) { auto boundView = QHttpServerRouterRule::bindCaptured(pageView, match); // it calls pageView boundView(); }));
Synopsis¶
Methods¶
def
__init__()def
addConverter()def
converters()def
handleRequest()
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__(server)¶
- Parameters:
server –
QAbstractHttpServer
Creates a
QHttpServerRouterobject with default converters.See also
Adds a new converter for
metaTypethat can be parsed withregexp. Having a converter for ametaTypeenables to use this type in a path pattern of aQHttpServerRouterRule. The regular expression is used to parse parameters of typemetaTypefrom the path pattern.If there is already a converter of type
metaType, that converter’s regexp is replaced withregexp.See also
- clearConverters()¶
Removes all converters.
- converters()¶
- Return type:
Dictionary with keys of type .QMetaType and values of type QString.
- handleRequest(request, responder)¶
- Parameters:
request –
QHttpServerRequestresponder –
QHttpServerResponder
- Return type:
bool
Handles each new
requestfor the HTTP server usingresponder.Iterates through the list of rules to find the first that matches, then executes this rule, returning
true. Returnsfalseif no rule matches the request.Removes the converter for type
metaType.See also