Sanic Server

sanic.http

class sanic.http.Http(protocol)

Bases: object

Internal helper for managing the HTTP request/response cycle

Raises
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

BaseHTTPResponse

class sanic.http.Stage(value)

Bases: enum.Enum

Enum for representing the stage of the request/response cycle

IDLE Waiting for request
REQUEST Request headers being received
HANDLER Headers done, handler running
RESPONSE Response headers sent, body in progress
FAILED 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.

Parameters

app (Sanic) –

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 (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

sanic.server.try_use_uvloop()

Use uvloop instead of the default asyncio loop.

Return type

None

sanic.worker