网页
综合
更新时间 2026-11-14 09:45:00
Endpoints - Starlette
自动蜘蛛
starlette.dev
正文约 1239 字
Starlette includes the classes HTTPEndpoint and WebSocketEndpoint that provide a class-based view pattern for handling HTTP method dispatching and WebSocket sessions.
The HTTPEndpoint class can be used as an ASGI application:
If you're using a Starlette application instance to handle routing, you can dispatch to an HTTPEndpoint class. Make sure to dispatch to the class itself, rather than to an instance of the class:
HTTP endpoint classes will respond with "405 Method not allowed" responses for any request methods which do not map to a corresponding handler. A get handler also serves HEAD requests, unless the endpoint defines its own head handler, so HEAD appears in the Allow header of those 405 responses.
The WebSocketEndpoint class is an ASGI application that presents a wrapper around the functionality of a WebSocket instance.
The ASGI connection scope is accessible on the endpoint instance via .scope and has an attribute encoding which may optionally be set, in order to validate the expected websocket data in the on_receive method.
The encoding types are:
There are three overridable methods for handling specific ASGI websocket message types:
The WebSocketEndpoint can also be used with the Starlette application class:
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。