
管理员
站长
- UID
- 1
- 积分
- 224398
- 余额
- 13 R
- Moe币
- 84879
- 在线时间
- 279 小时
- 注册时间
- 2025-12-28
- 最后登录
- 2026-11-6
|
from __future__ import annotations
import http
import ssl as ssl_module
import urllib.parse
from typing import Any, Awaitable, Callable, Literal
import trio
from ..http11 import Request, Response
from .server import Server, ServerConnection, serve
__all__ = ["route", "Router"]
try:
from werkzeug.exceptions import NotFound
from werkzeug.routing import Map, RequestRedirect
except ImportError:
async def route(
url_map: Map,
*args: Any,
server_name: str | None = None,
ssl: ssl_module.SSLContext | Literal[True] | None = None,
create_router: type[Router] | None = None,
task_status: trio.TaskStatus[Server] = trio.TASK_STATUS_IGNORED,
**kwargs: Any,
) -> None:
raise ImportError("route() requires werkzeug")
else:
async def route(
url_map: Map,
*args: Any,
server_name: str | None = None,
ssl: ssl_module.SSLContext | Literal[True] | None = None,
create_router: type[Router] | None = None,
task_status: trio.TaskStatus[Server] = trio.TASK_STATUS_IGNORED,
**kwargs: Any,
) -> None:
"""
Create a WebSocket server dispatching connections to different handlers.
This feature requires the third-party library `werkzeug`_:
.. code-block:: console
$ pip install werkzeug
.. _werkzeug: https://werkzeug.palletsprojects.com/
:func:`route` accepts the same arguments as
:func:`~websockets.trio.server.serve`, except as described below.
The first argument is a :class:`werkzeug.routing.Map` that maps URL patterns
to connection handlers. In addition to the connection, handlers receive
parameters captured in the URL as keyword arguments.
Here's an example::
from websockets.trio.router import route
from werkzeug.routing import Map, Rule
async def channel_handler(websocket, c |
|