new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

FastAPI · Reference · all subjects

httpconnection

9 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

HTTPConnection class import

HTTPConnection is a class that can be imported from fastapi.requests. It is used when defining dependencies that should be compatible with both HTTP and WebSockets.

HTTPConnection use case

HTTPConnection is used to define dependencies that are compatible with both HTTP connections (Request) and WebSocket connections. When you want a dependency to work with both types of connections, define a parameter that takes HTTPConnection instead of Request or WebSocket.

HTTPConnection import path

HTTPConnection should be imported using: from fastapi.requests import HTTPConnection

WebSocket parameter declaration syntax

To define WebSockets in FastAPI, declare a parameter of type WebSocket. With this parameter you can read data from the client and send data to it. The WebSocket class is provided by Starlette but can be imported directly from fastapi: from fastapi import WebSocket

HTTPConnection parameter for dual HTTP/WebSocket compatibility

When defining dependencies that should be compatible with both HTTP and WebSockets, use a parameter of type HTTPConnection instead of Request or WebSocket. This allows the same dependency to work with both HTTP and WebSocket connections.

WebSocket class members and methods

The WebSocket class includes the following members: scope, app, url, base_url, headers, query_params, path_params, cookies, client, state, url_for, client_state, application_state, receive, send, accept, receive_text, receive_bytes, receive_json, iter_text, iter_bytes, iter_json, send_text, send_bytes, send_json, close

WebSocketDisconnect exception for handling client disconnection

When a client disconnects from a WebSocket, a WebSocketDisconnect exception is raised. This exception can be caught to handle disconnection events. It can be imported directly from fastapi: from fastapi import WebSocketDisconnect

WebSocketState enumeration

WebSocketState is an enumeration of the possible states of a WebSocket connection. It can be imported from fastapi.websockets: from fastapi.websockets import WebSocketState

WebSocket imports from fastapi.websockets

Additional WebSocket-related classes can be imported from fastapi.websockets: from fastapi.websockets import WebSocketDisconnect, WebSocketState. These are provided directly by Starlette but available through FastAPI.

Give your agent this brain