Sanic Serverο
sanic.httpο
- class sanic.http.Http(protocol)ο
Bases:
object
Internal helper for managing the HTTP request/response cycle
- Raises
ServerError β
PayloadTooLarge β
Exception β
InvalidUsage β
RuntimeError β
ServerError β
ServerError β
InvalidUsage β
InvalidUsage β
InvalidUsage β
PayloadTooLarge β
RuntimeError β
- create_empty_request()ο
Current error handling code needs a request object that wonβt exist if an error occurred during before a request was received. Create a bogus response for error handling use.
- Return type
None
- async error_response(exception)ο
Handle response when exception encountered
- Parameters
exception (Exception) β
- Return type
None
- head_response_ignored(data, end_stream)ο
HEAD response: body data silently ignored.
- Parameters
data (bytes) β
end_stream (bool) β
- Return type
None
- async http1()ο
HTTP 1.1 connection handler
- async http1_request_header()ο
Receive and parse request header into self.request.
- async http1_response_chunked(data, end_stream)ο
Format a part of response body in chunked encoding.
- Parameters
data (bytes) β
end_stream (bool) β
- Return type
None
- async http1_response_normal(data, end_stream)ο
Format / keep track of non-chunked response.
- Parameters
data (bytes) β
end_stream (bool) β
- Return type
None
- init_for_request()ο
Init/reset all per-request variables.
- log_response()ο
Helper method provided to enable the logging of responses in case if the
HttpProtocol.access_log
is enabled.- Return type
None
- async read()ο
Read some bytes of request body.
- Return type
Optional[bytes]
- respond(response)ο
Initiate new streaming response.
Nothing is sent until the first send() call on the returned object, and calling this function multiple times will just alter the response to be given.
- Parameters
response (BaseHTTPResponse) β
- Return type
- class sanic.http.Stage(value)ο
Bases:
enum.Enum
Enum for representing the stage of the request/response cycle
IDLE
Waiting for requestREQUEST
Request headers being receivedHANDLER
Headers done, handler runningRESPONSE
Response headers sent, body in progressFAILED
Unrecoverable state (error while sending response)
sanic.serverο
- class sanic.server.AsyncioServer(app, loop, serve_coro, connections)ο
Bases:
object
Wraps an asyncio server with functionality that might be useful to a user who needs to manage the server lifecycle manually.
- after_start()ο
Trigger βafter_server_startβ events
- after_stop()ο
Trigger βafter_server_stopβ events
- before_start()ο
Trigger βbefore_server_startβ events
- before_stop()ο
Trigger βbefore_server_stopβ events
- startup()ο
Trigger βbefore_server_startβ events
- class sanic.server.ConnInfo(transport, unix=None)ο
Bases:
object
Local and remote addresses and SSL status info.
- Parameters
transport (sanic.models.protocol_types.TransportProtocol) β
- class sanic.server.HttpProtocol(*, loop, app, signal=None, connections=None, state=None, unix=None, **kwargs)ο
Bases:
sanic.server.protocols.base_protocol.SanicProtocol
This class provides implements the HTTP 1.1 protocol on top of our Sanic Server transport
- Parameters
app (Sanic) β
- check_timeouts()ο
Runs itself periodically to enforce any expired timeouts.
- close_if_idle()ο
Close the connection if a request is not being sent or received
- Returns
boolean - True if closed, false if staying open
- Return type
bool
- connection_made(transport)ο
HTTP-protocol-specific new connection handler
- async connection_task()ο
Run a HTTP connection.
Timeouts and some additional error handling occur here, while most of everything else happens in class Http or in code called from there.
- data_received(data)ο
Called when some data is received.
The argument is a bytes object.
- Parameters
data (bytes) β
- async send(data)ο
Writes HTTP data with backpressure control.
- sanic.server.serve(host, port, app, ssl=None, sock=None, unix=None, reuse_port=False, loop=None, protocol=<class 'sanic.server.protocols.http_protocol.HttpProtocol'>, backlog=100, register_sys_signals=True, run_multiple=False, run_async=False, connections=None, signal=<sanic.models.server_types.Signal object>, state=None, asyncio_server_kwargs=None)ο
Start asynchronous HTTP Server on an individual process.
- Parameters
host β Address to host on
port β Port to host on
before_start β function to be executed before the server starts listening. Takes arguments app instance and loop
after_start β function to be executed after the server starts listening. Takes arguments app instance and loop
before_stop β function to be executed when a stop signal is received before it is respected. Takes arguments app instance and loop
after_stop β function to be executed when a stop signal is received after it is respected. Takes arguments app instance and loop
ssl (Optional[SSLContext]) β SSLContext
sock (Optional[socket.socket]) β Socket for the server to accept connections from
unix (Optional[str]) β Unix socket to listen on instead of TCP port
reuse_port (bool) β True for multiple workers
loop β asyncio compatible event loop
run_async (bool) β bool: Do not create a new event loop for the server, and return an AsyncServer object rather than running it
asyncio_server_kwargs β key-value args for asyncio/uvloop create_server method
app (Sanic) β
protocol (Type[asyncio.Protocol]) β
backlog (int) β
register_sys_signals (bool) β
run_multiple (bool) β
- Returns
Nothing
- sanic.server.serve_multiple(server_settings, workers)ο
Start multiple server processes simultaneously. Stop on interrupt and terminate signals, and drain connections when complete.
- Parameters
server_settings β kw arguments to be passed to the serve function
workers β number of workers to launch
stop_event β if provided, is used as a stop signal
- Returns