connection

class litestar.connection.ASGIConnection[原始碼]

基底類別:Generic[HandlerT, UserT, AuthT, StateT]

The base ASGI connection container.

receive: Receive

ASGI 接收函式。

scope: Scope

The ASGI scope attached to the connection.

send: Send

ASGI 發送函式。

__init__(scope: Scope, receive: Receive = <function empty_receive>, send: Send = <function empty_send>) None[原始碼]

初始化 ASGIConnection

參數:
  • scope -- The ASGI connection scope.

  • receive -- ASGI 接收函式。

  • send -- ASGI 發送函式。

property app: Litestar

Return the app for this connection.

回傳:

The Litestar application instance

property auth: AuthT

Return the auth data of this connection's Scope.

引發:

ImproperlyConfiguredException -- If auth is not set in scope via an AuthMiddleware, raises an exception

回傳:

A type correlating to the generic variable Auth.

property base_url: URL

Return the base URL of this connection's Scope.

回傳:

A URL instance constructed from the request's scope, representing only the base part (host + domain + prefix) of the request.

clear_session() None[原始碼]

Remove the session from the connection's Scope.

If the Litestar SessionMiddleware is enabled, this will cause the session data to be cleared.

回傳:

None.

property client: Address | None

Return the client data of this connection's Scope.

回傳:

A two tuple of the host name and port number.

property cookies: dict[str, str]

Return the cookies of this connection's Scope.

回傳:

Returns any cookies stored in the header as a parsed dictionary.

property headers: Headers

Return the headers of this connection's Scope.

回傳:

A Headers instance with the request's scope["headers"] value.

property path_params: dict[str, Any]

Return the path_params of this connection's Scope.

回傳:

A string keyed dictionary of path parameter values.

property query_params: MultiDict[Any]

Return the query parameters of this connection's Scope.

回傳:

A normalized dict of query parameters. Multiple values for the same key are returned as a list.

property route_handler: HandlerT

Return the route_handler for this connection.

回傳:

The target route handler instance.

property session: dict[str, Any]

Return the session for this connection if a session was previously set in the Scope

回傳:

A dictionary representing the session value - if existing.

引發:

ImproperlyConfiguredException -- if session is not set in scope.

set_session(value: dict[str, Any] | DataContainerType | EmptyType) None[原始碼]

Set the session in the connection's Scope.

If the SessionMiddleware is enabled, the session will be added to the response as a cookie header.

參數:

value -- Dictionary or pydantic model instance for the session data.

回傳:

None

property state: StateT

Return the State of this connection.

回傳:

A State instance constructed from the scope["state"] value.

property url: URL

Return the URL of this connection's Scope.

回傳:

A URL instance constructed from the request's scope.

url_for(name: str | BaseRouteHandler, **path_parameters: Any) str[原始碼]

Return the url for a given route handler / handler name.

參數:
  • name -- The name of the route handler, or the route handler itself.

  • **path_parameters -- Values for path parameters in the route

引發:

NoRouteMatchFoundException -- If route with name does not exist, path parameters are missing or have a wrong type.

回傳:

A string representing the absolute url of the route handler.

property user: UserT

Return the user data of this connection's Scope.

引發:

ImproperlyConfiguredException -- If user is not set in scope via an AuthMiddleware, raises an exception

回傳:

A type correlating to the generic variable User.

class litestar.connection.Request[原始碼]

基底類別:Generic[UserT, AuthT, StateT], ASGIConnection[HTTPRouteHandler, UserT, AuthT, StateT]

The Litestar Request class.

__init__(scope: Scope, receive: Receive = <function empty_receive>, send: Send = <function empty_send>) None[原始碼]

初始化 Request

參數:
  • scope -- The ASGI connection scope.

  • receive -- ASGI 接收函式。

  • send -- ASGI 發送函式。

property accept: Accept

Parse the request's 'Accept' header, returning an Accept instance.

回傳:

An Accept instance, representing the list of acceptable media types.

async body() bytes[原始碼]

Return the body of the request.

回傳:

A byte-string representing the body of the request.

property content_type: tuple[str, dict[str, str]]

Parse the request's 'Content-Type' header, returning the header value and any options as a dictionary.

回傳:

A tuple with the parsed value and a dictionary containing any options send in it.

async form() FormMultiDict[原始碼]

Retrieve form data from the request. If the request is either a 'multipart/form-data' or an 'application/x-www-form- urlencoded', return a FormMultiDict instance populated with the values sent in the request, otherwise, an empty instance.

回傳:

A FormMultiDict instance

async json() Any[原始碼]

Retrieve the json request body from the request.

回傳:

An arbitrary value

property method: Method

Return the request method.

回傳:

The request Method

async msgpack() Any[原始碼]

Retrieve the MessagePack request body from the request.

回傳:

An arbitrary value

async send_push_promise(path: str, raise_if_unavailable: bool = False) None[原始碼]

Send a push promise.

This method requires the http.response.push extension to be sent from the ASGI server.

參數:
  • path -- Path to send the promise to.

  • raise_if_unavailable -- Raise an exception if server push is not supported by the server

回傳:

None

async stream() AsyncGenerator[bytes, None][原始碼]

Return an async generator that streams chunks of bytes.

回傳:

An async generator.

引發:

RuntimeError -- if the stream is already consumed

class litestar.connection.WebSocket[原始碼]

基底類別:Generic[UserT, AuthT, StateT], ASGIConnection[WebsocketRouteHandler, UserT, AuthT, StateT]

The Litestar WebSocket class.

__init__(scope: Scope, receive: Receive = <function empty_receive>, send: Send = <function empty_send>) None[原始碼]

初始化 WebSocket

參數:
  • scope -- The ASGI connection scope.

  • receive -- ASGI 接收函式。

  • send -- ASGI 發送函式。

async accept(subprotocols: str | None = None, headers: Headers | dict[str, Any] | list[tuple[bytes, bytes]] | None = None) None[原始碼]

Accept the incoming connection. This method should be called before receiving data.

參數:
  • subprotocols -- Websocket sub-protocol to use.

  • headers -- Headers to set on the data sent.

回傳:

None

async close(code: int = 1000, reason: str | None = None) None[原始碼]

Send an 'websocket.close' event.

參數:
  • code -- Status code.

  • reason -- Reason for closing the connection

回傳:

None

async iter_data(mode: WebSocketMode = 'text') AsyncGenerator[str | bytes, None][原始碼]

Continuously receive data and yield it

參數:

mode -- Socket mode to use. Either text or binary

async iter_json(mode: WebSocketMode = 'text') AsyncGenerator[Any, None][原始碼]

Continuously receive data and yield it, decoding it as JSON in the process.

參數:

mode -- Socket mode to use. Either text or binary

async iter_msgpack() AsyncGenerator[Any, None][原始碼]

Continuously receive data and yield it, decoding it as MessagePack in the process.

Note that since MessagePack is a binary format, this method will always receive data in binary mode.

async receive_bytes() bytes[原始碼]

Receive data as bytes.

回傳:

A byte-string.

async receive_data(mode: WebSocketMode) str | bytes[原始碼]

Receive an 'websocket.receive' event and returns the data stored on it.

參數:

mode -- The respective event key to use.

回傳:

The event's data.

async receive_json(mode: WebSocketMode = 'text') Any[原始碼]

Receive data and decode it as JSON.

參數:

mode -- Either text or binary.

回傳:

An arbitrary value

async receive_msgpack() Any[原始碼]

Receive data and decode it as MessagePack.

Note that since MessagePack is a binary format, this method will always receive data in binary mode.

回傳:

An arbitrary value

async receive_text() str[原始碼]

Receive data as text.

回傳:

A string.

receive_wrapper(receive: Receive) Receive[原始碼]

Wrap receive to set 'self.connection_state' and validate events.

參數:

receive -- ASGI 接收函式。

回傳:

An ASGI receive function.

async send_bytes(data: str | bytes, encoding: str = 'utf-8') None[原始碼]

Send data using the bytes key of the send event.

參數:
  • data -- Data to send

  • encoding -- Encoding to use for binary data.

回傳:

None

async send_data(data: str | bytes, mode: WebSocketMode = 'text', encoding: str = 'utf-8') None[原始碼]

Send a 'websocket.send' event.

參數:
  • data -- Data to send.

  • mode -- The respective event key to use.

  • encoding -- Encoding to use when converting bytes / str.

回傳:

None

async send_json(data: Any, mode: WebSocketMode = 'text', encoding: str = 'utf-8', serializer: Serializer = <function default_serializer>) None[原始碼]

Send data as JSON.

參數:
  • data -- A value to serialize.

  • mode -- Either text or binary.

  • encoding -- Encoding to use for binary data.

  • serializer -- A serializer function.

回傳:

None

async send_msgpack(data: Any, encoding: str = 'utf-8', serializer: Serializer = <function default_serializer>) None[原始碼]

Send data as MessagePack.

Note that since MessagePack is a binary format, this method will always send data in binary mode.

參數:
  • data -- A value to serialize.

  • encoding -- Encoding to use for binary data.

  • serializer -- A serializer function.

回傳:

None

async send_text(data: str | bytes, encoding: str = 'utf-8') None[原始碼]

Send data using the text key of the send event.

參數:
  • data -- Data to send

  • encoding -- Encoding to use for binary data.

回傳:

None

send_wrapper(send: Send) Send[原始碼]

Wrap send to ensure that state is not disconnected.

參數:

send -- ASGI 發送函式。

回傳:

An ASGI send function.