Getting Started¶
Here are some examples. More can be found in the github repository here.
All the examples use the uvicorn server.
The is further documentation on the types and utilities.
Simple¶
Here is a trivial example which registers a single http request callback at the endpoint /test
and responds with the plain text This is not a test
.
1 2 3 4 5 6 7 8 9 10 |
|
The callback is defined on lines 4-5.
The request
argument is of type HttpRequest
which has the following properties.
-
scope
is the dictionary passed straight through from the ASGI HTTP connection. -
info
is client data passed into the server on line 7. This provides a mechanism for passing data around the application without the need for global variables. -
context
is a private dictionary create for the lifetime of the request, or chain of requests. -
matches
contains a dictionary of the variables which matched parts of the request url. As this route definition contained nothing to match this will be empty. -
body
is an asynchronous iterator which provides the content of the request, and is not used here.
The callback returns an HttpResponse
. This was given a status code, a list of
headers, and a writer. The headers are supplied as a list of name-value byte
tuples. This is how the ASGI server expects them, so no extra work need be done.
The writer is an async iterator, meaning that the response supports streaming
from the ground up.
The application is created on line 7 with some client data containing the message to send.
The route to the callback function is defined on line 8. As this in an HTTP route (rather
than a websocket route) the http_router
is used. The first argument to the router is the set of methods
supported. These must be uppercase strings. The second argument as the path. This may contain matching
variables, but in this case is a simple absolute path. The last argument is the HTTP response
callback.
Finally the web server is started on line 10.
Rest Server¶
The following example provides a simple REST service. A GET on the /info
endpoint
returns the contents of the info
object, while a POST overwrites the INFO object with
the contents of the body converted from json.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
This example demonstrates how the body can be retrieved from the request on line 10.
Note how lightweight the response (on line 13) is, with simply the 204 (success no content) returned.
Websocket Handler¶
The following fragment shows a websocket callback.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
The handler takes a request
of type WebSocketRequest
. This provides the
following fields:
-
scope
is the dictionary passed straight through from the ASGI WebSocket connection. -
info
is client data passed into the server on line 7. This provides a mechanism for passing data around the application without the need for global variables. -
context
is a private dictionary create for the lifetime of the request, or chain of requests. -
matches
contains a dictionary of the variables which matched parts of the request url. As this route definition contained nothing to match this will be empty. -
web_socket
holds theWebSocket
.
The WebSocket
object provides four methods:
accept
- to accept the web socket. This must be called first.receive
- to read from the socket. When closed by the clientNone
is returned.send
- to write to the socket.close
- to close the socket.