quart.ctx 模块#

class quart.ctx.AppContext(app: Quart)#

基类: object

与绑定到当前任务的应用程序相关的上下文。

不要直接使用,优先使用 app_context()

app#

应用程序本身。

url_adapter#

绑定到服务器但不是特定任务的适配器,可用于路由构建。

g#

ctx 全局类的一个实例。

copy() AppContext#
async pop(exc: BaseException | None = <object object>) None#
async push() None#
class quart.ctx.RequestContext(app: Quart, request: Request, session: SessionMixin | None = None)#

基类: _BaseRequestWebsocketContext

与绑定到当前任务的特定请求相关的上下文。

不要直接使用,优先使用 request_context()test_request_context()

_after_request_functions#

在当前请求之后执行的函数列表,请参见 after_this_request()

async pop(exc: BaseException | None = <object object>) None#
async push() None#
property request: Request#
class quart.ctx.WebsocketContext(app: Quart, request: Websocket, session: SessionMixin | None = None)#

基类: _BaseRequestWebsocketContext

与绑定到当前任务的特定 websocket 相关的上下文。

不要直接使用,优先使用 websocket_context()test_websocket_context()

_after_websocket_functions#

在当前 websocket 之后执行的函数列表,请参见 after_this_websocket()

async pop(exc: BaseException | None = <object object>) None#
async push() None#
property websocket: Websocket#
quart.ctx.after_this_request(func: AfterRequestCallable) AfterRequestCallable#

计划在当前请求之后调用 func。

这在您只想为特定路由或情况使用请求后函数的情况下很有用,例如:

def index():
    @after_this_request
    def set_cookie(response):
        response.set_cookie('special', 'value')
        return response

    ...
quart.ctx.after_this_websocket(func: AfterWebsocketCallable) AfterWebsocketCallable#

计划在当前 websocket 之后调用 func。

这在您只想为特定路由或情况使用 websocket 后函数的情况下很有用,例如:

注意

响应是可选参数,仅当 websocket 未处于活动状态(即发生错误)时才会传递。

def index():
    @after_this_websocket
    def set_cookie(response: Optional[Response]):
        response.set_cookie('special', 'value')
        return response

    ...
quart.ctx.copy_current_app_context(func: Callable) Callable#

与装饰的函数共享当前应用程序上下文。

应用程序上下文是每个任务的本地上下文,因此在任何其他任务中都不可用。此装饰器可用于使上下文可用,

@copy_current_app_context
async def within_context() -> None:
    name = current_app.name
    ...
quart.ctx.copy_current_request_context(func: Callable) Callable#

与装饰的函数共享当前请求上下文。

请求上下文是每个任务的本地上下文,因此在任何其他任务中都不可用。此装饰器可用于使上下文可用,

@copy_current_request_context
async def within_context() -> None:
    method = request.method
    ...
quart.ctx.copy_current_websocket_context(func: Callable) Callable#

将当前 Websocket 上下文与装饰的函数共享。

Websocket 上下文是每个任务本地的,因此在任何其他任务中都不可用。此装饰器可用于使上下文可用。

@copy_current_websocket_context
async def within_context() -> None:
    method = websocket.method
    ...
quart.ctx.has_app_context() bool#

检查是否在应用程序上下文中执行。

这允许在有应用程序上下文可用时以受控方式执行操作,或者在没有应用程序上下文时静默不执行操作。例如:

if has_app_context():
    log.info("Executing in %s context", current_app.name)

另请参阅 has_request_context()

quart.ctx.has_request_context() bool#

检查是否在请求上下文中执行。

这允许在有请求上下文可用时以受控方式执行操作,或者在没有请求上下文时静默不执行操作。例如:

if has_request_context():
    log.info("Request endpoint %s", request.endpoint)

另请参阅 has_app_context().

quart.ctx.has_websocket_context() bool#

检查是否在 Websocket 上下文中执行。

这允许在有 Websocket 上下文可用时以受控方式执行操作,或者在没有 Websocket 上下文时静默不执行操作。例如:

if has_websocket_context():
    log.info("Websocket endpoint %s", websocket.endpoint)

另请参阅 has_app_context().