Sanic Documentation: Release 19.12.2
Sanic Documentation: Release 19.12.2
Sanic Documentation: Release 19.12.2
Release 19.12.2
Sanic contributors
2 Guides 5
2.1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.4 Request Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.5 Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.6 Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.7 Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.8 Blueprints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.9 Static Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.10 Versioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.11 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.12 Middleware And Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.13 WebSocket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.14 Handler Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.15 Streaming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.16 Class-Based Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.17 Custom Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.18 Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.19 SSL Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.20 Debug Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.21 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.22 Deploying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
2.23 Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.24 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.25 Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
2.26 Contributing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
2.27 API Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
2.28 Python 3.7 AsyncIO examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Index 137
i
ii
Sanic Documentation, Release 19.12.2
Sanic is a Python 3.6+ web server and web framework that’s written to go fast. It allows the usage of the async/await
syntax added in Python 3.5, which makes your code non-blocking and speedy.
The goal of the project is to provide a simple way to get up and running a highly performant HTTP server that is easy
to build, to expand, and ultimately to scale.
Sanic is developed on GitHub. Contributions are welcome!
CONTENTS 1
Sanic Documentation, Release 19.12.2
2 CONTENTS
CHAPTER
ONE
app = Sanic()
@app.route("/")
async def test(request):
return json({"hello": "world"})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
Note: Sanic does not support Python 3.5 from version 19.6 and forward. However, version 18.12LTS is supported
thru December 2020. Official Python support for version 3.5 is set to expire in September 2020.
3
Sanic Documentation, Release 19.12.2
TWO
GUIDES
Make sure you have both pip and at least version 3.6 of Python before starting. Sanic uses the new async/await syntax,
so earlier versions of python won’t work.
> If you are running on a clean install of Fedora 28 or above, please make sure you have the redhat-rpm-config
package installed in case if you want to use sanic with ujson dependency.
To install sanic without uvloop or ujson using bash, you can provide either or both of these environmental variables
using any truthy string like ‘y’, ‘yes’, ‘t’, ‘true’, ‘on’, ‘1’ and setting the SANIC_NO_X (X = UVLOOP/UJSON) to
true will stop that features installation.
app = Sanic()
@app.route("/")
async def test(request):
return json({"hello": "world"})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
5
Sanic Documentation, Release 19.12.2
python3 main.py
Open the address http://0.0.0.0:8000 in your web browser. You should see the message Hello world!.
You now have a working Sanic server!
2.2 Configuration
Any reasonably complex application will need configuration that is not baked into the actual code. Settings might be
different for different environments or installations.
2.2.1 Basics
Sanic holds the configuration in the config attribute of the application object. The configuration object is merely an
object that can be modified either using dot-notation or like a dictionary:
app = Sanic('myapp')
app.config.DB_NAME = 'appdb'
app.config.DB_USER = 'appuser'
Since the config object actually is a dictionary, you can use its update method in order to set several values at once:
db_settings = {
'DB_HOST': 'localhost',
'DB_NAME': 'appdb',
'DB_USER': 'appuser'
}
app.config.update(db_settings)
In general the convention is to only have UPPERCASE configuration parameters. The methods described below for
loading configuration only look for such uppercase parameters.
Any variables defined with the SANIC_ prefix will be applied to the sanic config. For example, setting
SANIC_REQUEST_TIMEOUT will be loaded by the application automatically and fed into the REQUEST_TIMEOUT
config variable. You can pass a different prefix to Sanic:
app = Sanic(load_env='MYAPP_')
Then the above variable would be MYAPP_REQUEST_TIMEOUT. If you want to disable loading from environment
variables you can set it to False instead:
6 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
app = Sanic(load_env=False)
From an Object
If there are a lot of configuration values and they have sensible defaults it might be helpful to put them into a module:
import myapp.default_settings
app = Sanic('myapp')
app.config.from_object(myapp.default_settings)
app = Sanic('myapp')
app.config.from_object('config.path.config.Class')
From a File
Usually you will want to load configuration from a file that is not part of the distributed application. You can load
configuration from a file using from_pyfile(/path/to/config_file). However, that requires the program to know the path
to the config file. So instead you can specify the location of the config file in an environment variable and tell Sanic to
use that to find the config file:
app = Sanic('myapp')
app.config.from_envvar('MYAPP_SETTINGS')
Then you can run your application with the MYAPP_SETTINGS environment variable set:
The config files are regular Python files which are executed in order to load them. This allows you to use arbitrary logic
for constructing the right configuration. Only uppercase variables are added to the configuration. Most commonly the
configuration consists of simple key value pairs:
# config_file
DB_HOST = 'localhost'
DB_NAME = 'appdb'
DB_USER = 'appuser'
Out of the box there are just a few predefined values which can be overwritten when creating the application.
2.2. Configuration 7
Sanic Documentation, Release 19.12.2
REQUEST_TIMEOUT
A request timeout measures the duration of time between the instant when a new open TCP connection is passed to
the Sanic backend server, and the instant when the whole HTTP request is received. If the time taken exceeds the
REQUEST_TIMEOUT value (in seconds), this is considered a Client Error so Sanic generates an HTTP 408 response
and sends that to the client. Set this parameter’s value higher if your clients routinely pass very large request payloads
or upload requests very slowly.
RESPONSE_TIMEOUT
A response timeout measures the duration of time between the instant the Sanic server passes the HTTP request to the
Sanic App, and the instant a HTTP response is sent to the client. If the time taken exceeds the RESPONSE_TIMEOUT
value (in seconds), this is considered a Server Error so Sanic generates an HTTP 503 response and sends that to
the client. Set this parameter’s value higher if your application is likely to have long-running process that delay the
generation of a response.
KEEP_ALIVE_TIMEOUT
What is Keep Alive? And what does the Keep Alive Timeout value do?
Keep-Alive is a HTTP feature introduced in HTTP 1.1. When sending a HTTP request, the client (usually a web
browser application) can set a Keep-Alive header to indicate the http server (Sanic) to not close the TCP connection
after it has send the response. This allows the client to reuse the existing TCP connection to send subsequent HTTP
requests, and ensures more efficient network traffic for both the client and the server.
The KEEP_ALIVE config variable is set to True in Sanic by default. If you don’t need this feature in your application,
set it to False to cause all client connections to close immediately after a response is sent, regardless of the Keep-Alive
header on the request.
8 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
The amount of time the server holds the TCP connection open is decided by the server itself. In Sanic, that value
is configured using the KEEP_ALIVE_TIMEOUT value. By default, it is set to 5 seconds. This is the same default
setting as the Apache HTTP server and is a good balance between allowing enough time for the client to send a new
request, and not holding open too many connections at once. Do not exceed 75 seconds unless you know your clients
are using a browser which supports TCP connections held open for that long.
For reference:
• Apache httpd server default keepalive timeout = 5 seconds
• Nginx server default keepalive timeout = 75 seconds
• Nginx performance tuning guidelines uses keepalive = 15 seconds
• IE (5-9) client hard keepalive limit = 60 seconds
• Firefox client hard keepalive limit = 115 seconds
• Opera 11 client hard keepalive limit = 120 seconds
• Chrome 13+ client keepalive limit > 300+ seconds
Proxy configuration
When you use a reverse proxy server (e.g. nginx), the value of request.ip will contain ip of a proxy, typically 127.0.0.1.
Sanic may be configured to use proxy headers for determining the true client IP, available as request.remote_addr. The
full external URL is also constructed from header fields if available.
Without proper precautions, a malicious client may use proxy headers to spoof its own IP. To avoid such issues, Sanic
does not use any proxy headers unless explicitly enabled.
Services behind reverse proxies must configure FORWARDED_SECRET, REAL_IP_HEADER and/or PROX-
IES_COUNT.
Forwarded header
2.2. Configuration 9
Sanic Documentation, Release 19.12.2
• a proxy that supports forwarded: set FORWARDED_SECRET to the value that the proxy inserts in the header
Earlier Sanic versions had unsafe default settings. From 19.9 onwards proxy settings must be set manually, and support
for negative PROXIES_COUNT has been removed.
2.3 Logging
Sanic allows you to do different types of logging (access log, error log) on the requests based on the python3 logging
API. You should have some basic knowledge on python3 logging if you want to create a new configuration.
app = Sanic('test')
@app.route('/')
async def test(request):
logger.info('Here is your log')
return text('Hello World!')
if __name__ == "__main__":
app.run(debug=True, access_log=True)
After the server is running, you can see some messages looks like:
You can send a request to server and it will print the log messages:
10 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
To use your own logging config, simply use logging.config.dictConfig, or pass log_config when you
initialize Sanic app:
if __name__ == "__main__":
app.run(access_log=False)
This would skip calling logging functions when handling requests. And you could even do further in production to
gain extra speed:
if __name__ == "__main__":
# disable debug messages
app.run(debug=False, access_log=False)
2.3.2 Configuration
Log format:
In addition to default parameters provided by python (asctime, levelname, message), Sanic provides additional
parameters for access logger with:
2.3. Logging 11
Sanic Documentation, Release 19.12.2
When an endpoint receives a HTTP request, the route function is passed a Request object.
The following variables are accessible as properties on Request objects:
• json (any) - JSON body
@app.route("/json")
def post_json(request):
return json({ "received": True, "message": request.json })
• args (dict) - Query string variables. A query string is the section of a URL that resembles ?
key1=value1&key2=value2.
If that URL were to be parsed, the args dictionary would look like {‘key1’: [‘value1’], ‘key2’: [‘value2’]}. The
request’s query_string variable holds the unparsed string value. Property is providing the default parsing strategy. If
you would like to change it look to the section below (Changing the default parsing rules of the queryset).
@app.route("/query_string")
def query_string(request):
return json({ "parsed": True, "args": request.args, "url": request.url, "query_
˓→string": request.query_string })
• query_args (list) - On many cases you would need to access the url arguments in a less packed form. query_args
is the list of (key, value) tuples.
Property is providing the default parsing strategy. If you would like to change it look to the section below (Changing
the default parsing rules of the queryset). For the same previous URL queryset ?key1=value1&key2=value2, the
query_args list would look like [(‘key1’, ‘value1’), (‘key2’, ‘value2’)]. And in case of the multiple params with
the same key like ?key1=value1&key2=value2&key1=value3 the query_args list would look like [(‘key1’, ‘value1’),
(‘key2’, ‘value2’), (‘key1’, ‘value3’)].
The difference between Request.args and Request.query_args for the queryset
?key1=value1&key2=value2&key1=value3
app = Sanic(__name__)
@app.route("/test_request_args")
async def test_request_args(request):
return json({
"parsed": True,
"url": request.url,
"query_string": request.query_string,
"args": request.args,
"raw_args": request.raw_args,
"query_args": request.query_args,
})
if __name__ == '__main__':
(continues on next page)
12 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
Output
{
"parsed":true,
"url":"http:\/\/0.0.0.0:8000\/test_request_args?key1=value1&key2=value2&
˓→key1=value3",
"query_string":"key1=value1&key2=value2&key1=value3",
"args":{"key1":["value1","value3"],"key2":["value2"]},
"raw_args":{"key1":"value1","key2":"value2"},
"query_args":[["key1","value1"],["key2","value2"],["key1","value3"]]
}
• raw_args contains only the first entry of key1. Will be deprecated in the future versions.
• files (dictionary of File objects) - List of files that have a name, body, and type
@app.route("/files")
def post_json(request):
test_file = request.files.get('test')
file_parameters = {
'body': test_file.body,
'name': test_file.name,
'type': test_file.type,
}
@app.route("/form")
def post_json(request):
return json({ "received": True, "form_data": request.form, "test": request.form.get(
˓→'test') })
• body (bytes) - Posted raw body. This property allows retrieval of the request’s raw data, regardless of content
type.
@app.route("/users", methods=["POST",])
def create_user(request):
return text("You are trying to create a user with the following POST: %s" %
˓→request.body)
bp = Blueprint('my_blueprint')
@bp.route('/')
async def bp_root(request):
if request.app.config['DEBUG']:
return json({'status': 'debug'})
else:
return json({'status': 'production'})
The default parameters that are using internally in args and query_args properties to parse queryset:
• keep_blank_values (bool): False - flag indicating whether blank values in percent-encoded queries should be
treated as blank strings. A true value indicates that blanks should be retained as blank strings. The default false
value indicates that blank values are to be ignored and treated as if they were not included.
• strict_parsing (bool): False - flag indicating what to do with parsing errors. If false (the default), errors are
silently ignored. If true, errors raise a ValueError exception.
• encoding and errors (str): ‘utf-8’ and ‘replace’ - specify how to decode percent-encoded sequences into Unicode
characters, as accepted by the bytes.decode() method.
If you would like to change that default parameters you could call get_args and get_query_args methods with the new
values.
For the queryset /?test1=value1&test2=&test3=value3:
14 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
@app.route("/query_string")
def query_string(request):
args_with_blank_values = request.get_args(keep_blank_values=True)
return json({
"parsed": True,
"url": request.url,
"args_with_blank_values": args_with_blank_values,
"query_string": request.query_string
})
{
"parsed": true,
"url": "http:\/\/0.0.0.0:8000\/query_string?test1=value1&test2=&test3=value3",
"args_with_blank_values": {"test1": ["value1"], "test2": "", "test3": ["value3"]},
"query_string": "test1=value1&test2=&test3=value3"
}
The request.args returns a subclass of dict called RequestParameters. The key difference when using this object is the
distinction between the get and getlist methods.
• get(key, default=None) operates as normal, except that when the value of the given key is a list, only the first
item is returned.
• getlist(key, default=None) operates as normal, returning the entire list.
args = RequestParameters()
args['titles'] = ['Post 1', 'Post 2']
The request.endpoint attribute holds the handler’s name. For instance, the below route will return “hello”.
app = Sanic()
@app.get("/")
def hello(request):
return text(request.endpoint)
Or, with a blueprint it will be include both, separated by a period. For example, the below route would return foo.bar:
app = Sanic(__name__)
blueprint = Blueprint('foo')
@blueprint.get('/')
async def bar(request):
return text(request.endpoint)
app.blueprint(blueprint)
2.5 Response
@app.route('/text')
def handle_request(request):
return response.text('Hello world!')
2.5.2 HTML
@app.route('/html')
def handle_request(request):
return response.html('<p>Hello world!</p>')
2.5.3 JSON
@app.route('/json')
def handle_request(request):
return response.json({'message': 'Hello world!'})
16 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
2.5.4 File
@app.route('/file')
async def handle_request(request):
return await response.file('/srv/www/whatever.png')
2.5.5 Streaming
@app.route("/streaming")
async def index(request):
async def streaming_fn(response):
await response.write('foo')
await response.write('bar')
return response.stream(streaming_fn, content_type='text/plain')
@app.route('/big_file.png')
async def handle_request(request):
return await response.file_stream('/srv/www/whatever.png')
2.5.7 Redirect
@app.route('/redirect')
def handle_request(request):
return response.redirect('/json')
2.5.8 Raw
@app.route('/raw')
def handle_request(request):
return response.raw(b'raw data')
2.5. Response 17
Sanic Documentation, Release 19.12.2
2.5.9 Empty
@app.route('/empty')
async def handle_request(request):
return response.empty()
To modify headers or status code, pass the headers or status argument to those functions:
@app.route('/json')
def handle_request(request):
return response.json(
{'message': 'Hello world!'},
headers={'X-Served-By': 'sanic'},
status=200
)
2.6 Cookies
Cookies are pieces of data which persist inside a user’s browser. Sanic can both read and write cookies, which are
stored as key-value pairs.
Warning: Cookies can be freely altered by the client. Therefore you cannot just store data such as login informa-
tion in cookies as-is, as they can be freely altered by the client. To ensure data you store in cookies is not forged or
tampered with by the client, use something like itsdangerous to cryptographically sign the data.
A user’s cookies can be accessed via the Request object’s cookies dictionary.
@app.route("/cookie")
async def test(request):
test_cookie = request.cookies.get('test')
return text("Test cookie set to: {}".format(test_cookie))
18 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
@app.route("/cookie")
async def test(request):
response = text("There's a cookie up in this response")
response.cookies['test'] = 'It worked!'
response.cookies['test']['domain'] = '.gotta-go-fast.com'
response.cookies['test']['httponly'] = True
return response
@app.route("/cookie")
async def test(request):
response = text("Time to eat some cookies muahaha")
return response
Response cookies can be set like dictionary values and have the following parameters available:
• expires (datetime): The time for the cookie to expire on the client’s browser.
• path (string): The subset of URLs to which this cookie applies. Defaults to /.
• comment (string): A comment (metadata).
• domain (string): Specifies the domain for which the cookie is valid. An explicitly specified domain must
always start with a dot.
• max-age (number): Number of seconds the cookie should live for.
• secure (boolean): Specifies whether the cookie will only be sent via HTTPS.
• httponly (boolean): Specifies whether the cookie cannot be read by Javascript.
2.6. Cookies 19
Sanic Documentation, Release 19.12.2
2.7 Routing
Routing allows the user to specify handler functions for different URL endpoints.
A basic route looks like the following, where app is an instance of the Sanic class:
@app.route("/")
async def test(request):
return json({ "hello": "world" })
When the url http://server.url/ is accessed (the base url of the server), the final / is matched by the router to the handler
function, test, which then returns a JSON object.
Sanic handler functions must be defined using the async def syntax, as they are asynchronous functions.
@app.route('/tag/<tag>')
async def tag_handler(request, tag):
return text('Tag - {}'.format(tag))
To specify a type for the parameter, add a :type after the parameter name, inside the quotes. If the parameter does not
match the specified type, Sanic will throw a NotFound exception, resulting in a 404: Page not found error on the URL.
Supported types
• string
– “Bob”
– “Python 3”
• int
– 10
– 20
– 30
– -10
– (No floats work here)
• number
– 1
– 1.5
– 10
20 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
– -10
• alpha
– “Bob”
– “Python”
– (If it contains a symbol or a non alphanumeric character it will fail)
• path
– “hello”
– “hello.text”
– “hello world”
• uuid
– 123a123a-a12a-1a1a-a1a1-1a12a1a12345 (UUIDv4 Support)
• regex expression
If no type is set then a string is expected. The argument given to the function will always be a string, independent of
the type.
@app.route('/string/<string_arg:string>')
async def string_handler(request, string_arg):
return text('String - {}'.format(string_arg))
@app.route('/int/<integer_arg:int>')
async def integer_handler(request, integer_arg):
return text('Integer - {}'.format(integer_arg))
@app.route('/number/<number_arg:number>')
async def number_handler(request, number_arg):
return text('Number - {}'.format(number_arg))
@app.route('/alpha/<alpha_arg:alpha>')
async def number_handler(request, alpha_arg):
return text('Alpha - {}'.format(alpha_arg))
@app.route('/path/<path_arg:path>')
async def number_handler(request, path_arg):
return text('Path - {}'.format(path_arg))
@app.route('/uuid/<uuid_arg:uuid>')
async def number_handler(request, uuid_arg):
return text('Uuid - {}'.format(uuid_arg))
@app.route('/person/<name:[A-z]+>')
async def person_handler(request, name):
return text('Person - {}'.format(name))
@app.route('/folder/<folder_id:[A-z0-9]{0,4}>')
async def folder_handler(request, folder_id):
return text('Folder - {}'.format(folder_id))
2.7. Routing 21
Sanic Documentation, Release 19.12.2
Warning: str is not a valid type tag. If you want str recognition then you must use string
By default, a route defined on a URL will be available for only GET requests to that URL. However, the @app.route
decorator accepts an optional parameter, methods, which allows the handler function to work with any of the HTTP
methods in the list.
@app.route('/post', methods=['POST'])
async def post_handler(request):
return text('POST request - {}'.format(request.json))
@app.route('/get', methods=['GET'])
async def get_handler(request):
return text('GET request - {}'.format(request.args))
There is also an optional host argument (which can be a list or a string). This restricts a route to the host or hosts
provided. If there is a also a route with no host, it will be the default.
# if the host header doesn't match example.com, this route will be used
@app.route('/get', methods=['GET'])
async def get_handler(request):
return text('GET request in default - {}'.format(request.args))
@app.post('/post')
async def post_handler(request):
return text('POST request - {}'.format(request.json))
@app.get('/get')
async def get_handler(request):
return text('GET request - {}'.format(request.args))
As we have seen, routes are often specified using the @app.route decorator. However, this decorator is really just a
wrapper for the app.add_route method, which is used as follows:
22 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
Sanic provides a url_for method, to generate URLs based on the handler method name. This is useful if you want to
avoid hardcoding url paths into your app; instead, you can just reference the handler name. For example:
from sanic.response import redirect
@app.route('/')
async def index(request):
# generate a URL for the endpoint `post_handler`
url = app.url_for('post_handler', post_id=5)
# the URL is `/posts/5`, redirect to it
return redirect(url)
@app.route('/posts/<post_id>')
async def post_handler(request, post_id):
return text('Post - {}'.format(post_id))
• Also some special arguments (_anchor, _external, _scheme, _method, _server) passed to url_for will have
special url building (_method is not supported now and will be ignored). For example:
url = app.url_for('post_handler', post_id=5, arg_one='one', _anchor='anchor')
# /posts/5?arg_one=one#anchor
2.7. Routing 23
Sanic Documentation, Release 19.12.2
# http://another_server:8888/posts/5?arg_one=one&arg_one=two&arg_two=2#anchor
• All valid parameters must be passed to url_for to build a URL. If a parameter is not supplied, or if a parameter
does not match the specified type, a URLBuildError will be raised.
Routes for the WebSocket protocol can be defined with the @app.websocket decorator:
@app.websocket('/feed')
async def feed(request, ws):
while True:
data = 'hello!'
print('Sending: ' + data)
await ws.send(data)
data = await ws.recv()
print('Received: ' + data)
app.add_websocket_route(my_websocket_handler, '/feed')
Handlers to a WebSocket route are invoked with the request as first argument, and a WebSocket protocol object as
second argument. The protocol object has send and recv methods to send and receive data respectively.
WebSocket support requires the websockets package by Aymeric Augustin.
You can make routes strict to trailing slash or not, it’s configurable.
@bp.get('/bp/get', strict_slashes=False)
def handler(request):
return text('OK')
(continues on next page)
24 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
app.blueprint(bp)
The behavior of how the strict_slashes flag follows a defined hierarchy which decides if a specific route falls under
the strict_slashes behavior.
Route/
Blueprint/
Application/
Above hierarchy defines how the strict_slashes flag will behave. The first non None value of the strict_slashes found
in the above order will be applied to the route in question.
from sanic import Sanic, Blueprint
from sanic.response import text
@app.get("/r1")
def r1(request):
return text("strict_slashes is applicable from App level")
@app.get("/r2", strict_slashes=False)
def r2(request):
return text("strict_slashes is not applicable due to False value set in route
˓→level")
bp = Blueprint("bp", strict_slashes=False)
@bp.get("/r3", strict_slashes=True)
def r3(request):
return text("strict_slashes applicable from blueprint route level")
@bp.get("/r4")
def r3(request):
return text("strict_slashes applicable from blueprint level")
A custom route name can be used by passing a name argument while registering the route which will override the
default route name generated using the handler.__name__ attribute.
app = Sanic('test_named_route')
@app.get('/get', name='get_handler')
def handler(request):
return text('OK')
2.7. Routing 25
Sanic Documentation, Release 19.12.2
@bp.get('/bp/get', name='get_handler')
def handler(request):
return text('OK')
app.blueprint(bp)
# different names can be used for same url with different methods
@app.get('/test', name='route_test')
def handler(request):
return text('OK')
@app.post('/test', name='route_post')
def handler2(request):
return text('OK POST')
@app.put('/test', name='route_put')
def handler3(request):
return text('OK PUT')
# below url are the same, you can use any of them
# '/test'
app.url_for('route_test')
# app.url_for('route_post')
# app.url_for('route_put')
@app.post('/post', name='post_handler')
def handler(request):
return text('OK')
# then
# app.url_for('handler') == '/get'
# app.url_for('post_handler') == '/post'
26 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
Sanic supports using url_for method to build static file urls. In case if the static url is pointing to a directory, filename
parameter to the url_for can be ignored. q
app = Sanic('test_static')
app.static('/static', './static')
app.static('/uploads', './uploads', name='uploads')
app.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')
bp = Blueprint('bp', url_prefix='bp')
bp.static('/static', './static')
bp.static('/uploads', './uploads', name='uploads')
bp.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')
app.blueprint(bp)
2.8 Blueprints
Blueprints are objects that can be used for sub-routing within an application. Instead of adding routes to the application
instance, blueprints define similar methods for adding routes, which are then registered with the application in a flexible
and pluggable manner.
Blueprints are especially useful for larger applications, where your application logic can be broken down into several
groups or areas of responsibility.
The following shows a very simple blueprint that registers a handler-function at the root / of your application.
Suppose you save this file as my_blueprint.py, which can be imported into your main application later.
bp = Blueprint('my_blueprint')
@bp.route('/')
async def bp_root(request):
return json({'my': 'blueprint'})
2.8. Blueprints 27
Sanic Documentation, Release 19.12.2
app = Sanic(__name__)
app.blueprint(bp)
This will add the blueprint to the application and register any routes defined by that blueprint. In this example, the
registered routes in the app.router will look like:
Blueprints may also be registered as part of a list or tuple, where the registrar will recursively cycle through any
sub-sequences of blueprints and register them accordingly. The Blueprint.group method is provided to simplify this
process, allowing a ‘mock’ backend directory structure mimicking what’s seen from the front end. Consider this (quite
contrived) example:
api/
content/
authors.py
static.py
__init__.py
info.py
__init__.py
app.py
# api/content/authors.py
from sanic import Blueprint
# api/content/static.py
from sanic import Blueprint
# api/content/__init__.py
from sanic import Blueprint
28 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
# api/info.py
from sanic import Blueprint
# api/__init__.py
from sanic import Blueprint
And registering these blueprints in app.py can now be done like so:
# app.py
from sanic import Sanic
app = Sanic(__name__)
app.blueprint(api)
WebSocket routes
WebSocket handlers can be registered on a blueprint using the @bp.websocket decorator or bp.add_websocket_route
method.
Blueprint Middleware
@bp.middleware
async def print_on_request(request):
print("I am a spy")
@bp.middleware('request')
async def halt_request(request):
return text('I halted the request')
@bp.middleware('response')
async def halt_response(request, response):
return text('I halted the response')
2.8. Blueprints 29
Sanic Documentation, Release 19.12.2
Using this middleware will ensure that you can apply a common middleware to all the blueprints that form the current
blueprint group under consideration.
@bp1.middleware('request')
async def bp1_only_middleware(request):
print('applied on Blueprint : bp1 Only')
@bp1.route('/')
async def bp1_route(request):
return text('bp1')
@bp2.route('/<param>')
async def bp2_route(request, param):
return text(param)
@group.middleware('request')
async def group_middleware(request):
print('common middleware applied for both bp1 and bp2')
Exceptions
@bp.exception(NotFound)
def ignore_404s(request, exception):
return text("Yep, I totally found the page: {}".format(request.url))
Static files
bp.static('/web/path', '/folder/to/serve')
# also you can pass name parameter to it for url_for
bp.static('/web/path', '/folder/to/server', name='uploads')
app.url_for('static', name='bp.uploads', filename='file.txt') == '/bp/web/path/file.
˓→txt'
30 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
Blueprints can run functions during the start and stop process of the server. If running in multiprocessor mode (more
than 1 worker), these are triggered after the workers fork.
Available events are:
• before_server_start: Executed before the server begins to accept connections
• after_server_start: Executed after the server begins to accept connections
• before_server_stop: Executed before the server stops accepting connections
• after_server_stop: Executed after the server is stopped and all requests are complete
bp = Blueprint('my_blueprint')
@bp.listener('before_server_start')
async def setup_connection(app, loop):
global database
database = mysql.connect(host='127.0.0.1'...)
@bp.listener('after_server_stop')
async def close_connection(app, loop):
await database.close()
Blueprints can be very useful for API versioning, where one blueprint may point at /v1/<routes>, and another pointing
at /v2/<routes>.
When a blueprint is initialised, it can take an optional version argument, which will be prepended to all routes defined
on the blueprint. This feature can be used to implement our API versioning scheme.
# blueprints.py
from sanic.response import text
from sanic import Blueprint
@blueprint_v1.route('/')
async def api_v1_root(request):
return text('Welcome to version 1 of our documentation')
@blueprint_v2.route('/')
async def api_v2_root(request):
return text('Welcome to version 2 of our documentation')
When we register our blueprints on the app, the routes /v1/api and /v2/api will now point to the individual blueprints,
which allows the creation of sub-sites for each API version.
# main.py
from sanic import Sanic
from blueprints import blueprint_v1, blueprint_v2
app = Sanic(__name__)
app.blueprint(blueprint_v1)
(continues on next page)
2.8. Blueprints 31
Sanic Documentation, Release 19.12.2
If you wish to generate a URL for a route inside of a blueprint, remember that the endpoint name takes the format
<blueprint_name>.<handler_name>. For example:
@blueprint_v1.route('/')
async def root(request):
url = request.app.url_for('v1.post_handler', post_id=5) # --> '/v1/api/post/5'
return redirect(url)
@blueprint_v1.route('/post/<post_id>')
async def post_handler(request, post_id):
return text('Post {} in Blueprint V1'.format(post_id))
Static files and directories, such as an image file, are served by Sanic when registered with the app.static() method.
The method takes an endpoint URL and a filename. The file specified will then be accessible via the given endpoint.
app = Sanic(__name__)
32 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
app.run(host="0.0.0.0", port=8000)
> Note: Sanic does not provide directory index when you serve a static directory.
The app.static() method also support virtual host. You can serve your static files with specific virtual host with host
argument. For example:
app = Sanic(__name__)
app.static('/static', './static')
app.static('/example_static', './example_static', host='www.example.com')
In some cases, you might server large file(ex: videos, images, etc.) with Sanic. You can choose to use streaming file
rather than download directly.
Here is an example:
app = Sanic(__name__)
When stream_large_files is True, Sanic will use file_stream() instead of file() to serve static files. This will use 1KB
as the default chunk size. And, if needed, you can also use a custom chunk size. For example:
app = Sanic(__name__)
2.10 Versioning
You can pass the version keyword to the route decorators, or to a blueprint initializer. It will result in the v{version}
url prefix where {version} is the version number.
@app.route('/text', version=1)
def handle_request(request):
return response.text('Hello world! Version 1')
@app.route('/text', version=2)
def handle_request(request):
return response.text('Hello world! Version 2')
app.run(port=80)
curl localhost/v1/text
curl localhost/v2/text
You can also pass a version number to the blueprint, which will apply to all routes.
bp = Blueprint('test', version=1)
@bp.route('/html')
def handle_request(request):
return response.html('<p>Hello world!</p>')
34 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
curl localhost/v1/html
2.11 Exceptions
Exceptions can be thrown from within request handlers and will automatically be handled by Sanic. Exceptions take
a message as their first argument, and can also take a status code to be passed back in the HTTP response.
To throw an exception, simply raise the relevant exception from the sanic.exceptions module.
@app.route('/killme')
async def i_am_ready_to_die(request):
raise ServerError("Something bad happened", status_code=500)
You can also use the abort function with the appropriate status code:
@app.route('/youshallnotpass')
async def no_no(request):
abort(401)
# this won't happen
text("OK")
To override Sanic’s default handling of an exception, the @app.exception decorator is used. The decorator expects a
list of exceptions to handle as arguments. You can pass SanicException to catch them all! The decorated exception
handler function must take a Request and Exception object as arguments.
@app.exception(NotFound)
async def ignore_404s(request, exception):
return text("Yep, I totally found the page: {}".format(request.url))
app = Sanic()
app.error_handler.add(Exception, server_error_handler)
2.11. Exceptions 35
Sanic Documentation, Release 19.12.2
In some cases, you might want to add some more error handling functionality to what is provided by default. In that
case, you can subclass Sanic’s default error handler as such:
class CustomErrorHandler(ErrorHandler):
def default(self, request, exception):
''' handles errors that have no error handlers assigned '''
# You custom error handling logic...
return super().default(request, exception)
app = Sanic()
app.error_handler = CustomErrorHandler()
Middleware are functions which are executed before or after requests to the server. They can be used to modify the
request to or response from user-defined handler functions.
Additionally, Sanic provides listeners which allow you to run code at various points of your application’s lifecycle.
2.12.1 Middleware
There are two types of middleware: request and response. Both are declared using the @app.middleware decorator,
with the decorator’s parameter being a string representing its type: ‘request’ or ‘response’.
• Request middleware receives only the request as argument.
• Response middleware receives both the request and response.
The simplest middleware doesn’t modify the request or response at all:
@app.middleware('request')
async def print_on_request(request):
print("I print when a request is received by the server")
@app.middleware('response')
async def print_on_response(request, response):
print("I print when a response is returned by the server")
36 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
Middleware can modify the request or response parameter it is given, as long as it does not return it. The following
example shows a practical use-case for this.
app = Sanic(__name__)
@app.middleware('request')
async def add_key(request):
# Arbitrary data may be stored in request context:
request.ctx.foo = 'bar'
@app.middleware('response')
async def custom_banner(request, response):
response.headers["Server"] = "Fake-Server"
@app.middleware('response')
async def prevent_xss(request, response):
response.headers["x-xss-protection"] = "1; mode=block"
@app.get("/")
async def index(request):
return sanic.response.text(request.ctx.foo)
app.run(host="0.0.0.0", port=8000)
If middleware returns a HTTPResponse object, the request will stop processing and the response will be returned. If
this occurs to a request before the relevant user route handler is reached, the handler will never be called. Returning a
response will also prevent any further middleware from running.
@app.middleware('request')
async def halt_request(request):
return text('I halted the request')
@app.middleware('response')
async def halt_response(request, response):
return text('I halted the response')
Arbitrary data may be stored in request.ctx. A typical use case would be to store the user object acquired from
database in an authentication middleware. Keys added are accessible to all later middleware as well as the handler
over the duration of the request.
Custom context is reserved for applications and extensions. Sanic itself makes no use of it.
2.12.5 Listeners
If you want to execute startup/teardown code as your server starts or closes, you can use the following listeners:
• before_server_start
• after_server_start
• before_server_stop
• after_server_stop
These listeners are implemented as decorators on functions which accept the app object as well as the asyncio loop.
For example:
@app.listener('before_server_start')
async def setup_db(app, loop):
app.db = await db_setup()
@app.listener('after_server_start')
async def notify_server_started(app, loop):
print('Server successfully started!')
@app.listener('before_server_stop')
async def notify_server_stopping(app, loop):
print('Server shutting down!')
@app.listener('after_server_stop')
async def close_db(app, loop):
await app.db.close()
It’s also possible to register a listener using the register_listener method. This may be useful if you define your listeners
in another module besides the one you instantiate your app in.
app = Sanic()
app.register_listener(setup_db, 'before_server_start')
If you want to schedule a background task to run after the loop has started, Sanic provides the add_task method to
easily do so.
app.add_task(notify_server_started_after_five_seconds())
38 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
Sanic will attempt to automatically inject the app, passing it as an argument to the task:
app.add_task(notify_server_started_after_five_seconds)
Or you can pass the app explicitly for the same effect:
app.add_task(notify_server_started_after_five_seconds(app))
2.13 WebSocket
Sanic provides an easy to use abstraction on top of websockets. Sanic Supports websocket versions 7 and 8.
To setup a WebSocket:
app = Sanic()
@app.websocket('/feed')
async def feed(request, ws):
while True:
data = 'hello!'
print('Sending: ' + data)
await ws.send(data)
data = await ws.recv()
print('Received: ' + data)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, protocol=WebSocketProtocol)
app.add_websocket_route(feed, '/feed')
Handlers for a WebSocket route is invoked with the request as first argument, and a WebSocket protocol object as
second argument. The protocol object has send and recv methods to send and receive data respectively.
You could setup your own WebSocket configuration through app.config, like
app.config.WEBSOCKET_MAX_SIZE = 2 ** 20
app.config.WEBSOCKET_MAX_QUEUE = 32
app.config.WEBSOCKET_READ_LIMIT = 2 ** 16
app.config.WEBSOCKET_WRITE_LIMIT = 2 ** 16
2.13. WebSocket 39
Sanic Documentation, Release 19.12.2
Since Sanic handlers are simple Python functions, you can apply decorators to them in a similar manner to Flask. A
typical use case is when you want some code to run before a handler’s code is executed.
Let’s say you want to check that a user is authorized to access a particular endpoint. You can create a decorator that
wraps a handler function, checks a request if the client is authorized to access a resource, and sends the appropriate
response.
def authorized():
def decorator(f):
@wraps(f)
async def decorated_function(request, *args, **kwargs):
# run some method that checks the request
# for the client's authorization status
is_authorized = check_request_for_authorization_status(request)
if is_authorized:
# the user is authorized.
# run the handler method and return the response
response = await f(request, *args, **kwargs)
return response
else:
# the user is not authorized.
return json({'status': 'not_authorized'}, 403)
return decorated_function
return decorator
@app.route("/")
@authorized()
async def test(request):
return json({'status': 'authorized'})
2.15 Streaming
Sanic allows you to get request data by stream, as below. When the request ends, await request.stream.read() returns
None. Only post, put and patch decorator have stream argument.
40 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
bp = Blueprint('blueprint_request_stream')
app = Sanic('request_stream')
class SimpleView(HTTPMethodView):
@stream_decorator
async def post(self, request):
result = ''
while True:
body = await request.stream.read()
if body is None:
break
result += body.decode('utf-8')
return text(result)
@app.post('/stream', stream=True)
async def handler(request):
async def streaming(response):
while True:
body = await request.stream.read()
if body is None:
break
body = body.decode('utf-8').replace('1', 'A')
await response.write(body)
return stream(streaming)
@bp.put('/bp_stream', stream=True)
async def bp_put_handler(request):
result = ''
while True:
body = await request.stream.read()
if body is None:
break
result += body.decode('utf-8').replace('1', 'A')
return text(result)
2.15. Streaming 41
Sanic Documentation, Release 19.12.2
app.blueprint(bp)
app.add_route(SimpleView.as_view(), '/method_view')
view = CompositionView()
view.add(['POST'], post_handler, stream=True)
app.add_route(view, '/composition_view')
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8000)
Sanic allows you to stream content to the client with the stream method. This method accepts a coroutine callback
which is passed a StreamingHTTPResponse object that is written to. A simple example is like follows:
app = Sanic(__name__)
@app.route("/")
async def test(request):
async def sample_streaming_fn(response):
await response.write('foo,')
await response.write('bar')
This is useful in situations where you want to stream content to the client that originates in an external service, like
a database. For example, you can stream database records to the client with the asynchronous cursor that asyncpg
provides:
@app.route("/")
async def index(request):
async def stream_from_db(response):
conn = await asyncpg.connect(database='test')
async with conn.transaction():
async for record in conn.cursor('SELECT generate_series(0, 10)'):
await response.write(record[0])
return stream(stream_from_db)
If a client supports HTTP/1.1, Sanic will use chunked transfer encoding; you can explicitly enable or disable it using
chunked option of the stream function.
42 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
Sanic provides sanic.response.file_stream function that is useful when you want to send a large file. It returns a
StreamingHTTPResponse object and will use chunked transfer encoding by default; for this reason Sanic doesn’t
add Content-Length HTTP header in the response. If you want to use this header, you can disable chunked transfer
encoding and add it manually:
from aiofiles import os as async_os
from sanic.response import file_stream
@app.route("/")
async def index(request):
file_path = "/srv/www/whatever.png"
Class-based views are simply classes which implement response behaviour to requests. They provide a way to com-
partmentalise handling of different HTTP request types at the same endpoint. Rather than defining and decorating
three different handler functions, one for each of an endpoint’s supported request type, the endpoint can be assigned a
class-based view.
A class-based view should subclass HTTPMethodView. You can then implement class methods for every HTTP request
type you want to support. If a request is received that has no defined method, a 405: Method not allowed response
will be generated.
To register a class-based view on an endpoint, the app.add_route method is used. The first argument should be the
defined class with the method as_view invoked, and the second should be the URL endpoint.
The available methods are get, post, put, patch, and delete. A class using all these methods would look like the
following.
from sanic import Sanic
from sanic.views import HTTPMethodView
from sanic.response import text
app = Sanic('some_name')
class SimpleView(HTTPMethodView):
app.add_route(SimpleView.as_view(), '/')
app = Sanic('some_name')
class SimpleAsyncView(HTTPMethodView):
app.add_route(SimpleAsyncView.as_view(), '/')
If you need any URL parameters, as discussed in the routing guide, include them in the method definition.
class NameView(HTTPMethodView):
app.add_route(NameView.as_view(), '/<name>')
2.16.3 Decorators
If you want to add any decorators to the class, you can set the decorators class variable. These will be applied to the
class when as_view is called.
class ViewWithDecorator(HTTPMethodView):
decorators = [some_decorator_here]
app.add_route(ViewWithDecorator.as_view(), '/url')
44 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
But if you just want to decorate some functions and not all functions, you can do as follows:
class ViewWithSomeDecorator(HTTPMethodView):
@staticmethod
@some_decorator_here
def get(request, name):
return text("Hello I have a decorator")
If you wish to build a URL for an HTTPMethodView, remember that the class name will be the endpoint that you will
pass into url_for. For example:
@app.route('/')
def index(request):
url = app.url_for('SpecialClassView')
return redirect(url)
class SpecialClassView(HTTPMethodView):
def get(self, request):
return text('Hello from the Special Class View!')
app.add_route(SpecialClassView.as_view(), '/special_class_view')
As an alternative to the HTTPMethodView, you can use CompositionView to move handler functions outside of the
view class.
Handler functions for each supported HTTP method are defined elsewhere in the source, and then added to the view
using the CompositionView.add method. The first parameter is a list of HTTP methods to handle (e.g. [‘GET’,
‘POST’]), and the second is the handler function. The following example shows CompositionView usage with both an
external handler function and an inline lambda:
app = Sanic(__name__)
def get_handler(request):
return text('I am a get method')
view = CompositionView()
view.add(['GET'], get_handler)
view.add(['POST', 'PUT'], lambda request: text('I am a post/put method'))
Note: currently you cannot build a URL for a CompositionView using url_for.
Note: This is advanced usage, and most readers will not need such functionality.
You can change the behavior of Sanic’s protocol by specifying a custom protocol, which should be a subclass of
asyncio.protocol. This protocol can then be passed as the keyword argument protocol to the sanic.run method.
The constructor of the custom protocol class receives the following keyword arguments from Sanic.
• loop: an asyncio-compatible event loop.
• connections: a set to store protocol objects. When Sanic receives SIGINT or SIGTERM, it executes
protocol.close_if_idle for all protocol objects stored in this set.
• signal: a sanic.server.Signal object with the stopped attribute. When Sanic receives SIGINT or
SIGTERM, signal.stopped is assigned True.
• request_handler: a coroutine that takes a sanic.request.Request object and a response call-
back as arguments.
• error_handler: a sanic.exceptions.Handler which is called when exceptions are raised.
• request_timeout: the number of seconds before a request times out.
• request_max_size: an integer specifying the maximum size of a request, in bytes.
2.17.1 Example
An error occurs in the default protocol if a handler function does not return an HTTPResponse object.
By overriding the write_response protocol method, if a handler returns a string it will be converted to an
HTTPResponse object.
from sanic import Sanic
from sanic.server import HttpProtocol
from sanic.response import text
app = Sanic(__name__)
class CustomHttpProtocol(HttpProtocol):
46 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
@app.route('/')
async def string(request):
return 'string'
@app.route('/1')
async def response(request):
return text('response')
2.18 Sockets
Sanic can use the python socket module to accommodate non IPv4 sockets.
IPv6 example:
from sanic import Sanic
from sanic.response import json
import socket
app = Sanic()
@app.route("/")
async def test(request):
return json({"hello": "world"})
if __name__ == "__main__":
app.run(sock=sock)
server_socket = '/tmp/sanic.sock'
2.18. Sockets 47
Sanic Documentation, Release 19.12.2
app = Sanic()
@app.route("/")
async def test(request):
return json({"hello": "world"})
if __name__ == "__main__":
app.run(sock=sock)
import ssl
context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain("/path/to/cert", keyfile="/path/to/keyfile")
You can also pass in the locations of a certificate and key as a dictionary:
When enabling Sanic’s debug mode, Sanic will provide a more verbose logging output and by default will enable the
Auto Reload feature.
Warning: Sanic’s debug more will slow down the server’s performance and is therefore advised to enable it only
in development environments.
48 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
By setting the debug mode a more verbose output from Sanic will be output and the Automatic Reloader will be
activated.
app = Sanic()
@app.route('/')
async def hello_world(request):
return json({"hello": "world"})
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000, debug=True)
Sanic offers a way to enable or disable the Automatic Reloader manually, the auto_reload argument will activate
or deactivate the Automatic Reloader.
app = Sanic()
@app.route('/')
async def hello_world(request):
return json({"hello": "world"})
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000, auto_reload=True)
2.21 Testing
Sanic endpoints can be tested locally using the test_client object, which depends on an additional package: httpx
library, which implements an API that mirrors the requests library.
The test_client exposes get, post, put, delete, patch, head and options methods for you to run against your application.
A simple example (using pytest) is like follows:
def test_index_returns_200():
request, response = app.test_client.get('/')
assert response.status == 200
def test_index_put_not_allowed():
request, response = app.test_client.put('/')
assert response.status == 405
2.21. Testing 49
Sanic Documentation, Release 19.12.2
Internally, each time you call one of the test_client methods, the Sanic app is run at 127.0.0.1:42101 and your test
request is executed against your application, using httpx.
The test_client methods accept the following arguments and keyword arguments:
• uri (default `’/’`) A string representing the URI to test.
• gather_request (default `True`) A boolean which determines whether the original request will be returned by the
function. If set to True, the return value is a tuple of (request, response), if False only the response is returned.
• server_kwargs (default `{}`) a dict of additional arguments to pass into app.run before the test request is run.
• debug (default `False`) A boolean which determines whether to run the server in debug mode.
The function further takes the *request_args and **request_kwargs, which are passed directly to the request.
For example, to supply data to a GET request, you would do the following:
def test_get_request_includes_data():
params = {'key1': 'value1', 'key2': 'value2'}
request, response = app.test_client.get('/', params=params)
assert request.args.get('key1') == 'value1'
def test_post_json_request_includes_data():
data = {'key1': 'value1', 'key2': 'value2'}
request, response = app.test_client.post('/', data=json.dumps(data))
assert request.json.get('key1') == 'value1'
More information about the available arguments to httpx can be found [in the documentation for httpx.
If you need to test using a free unpriveleged port chosen by the kernel instead of the default with SanicTestClient, you
can do so by specifying port=None. On most systems the port will be in the range 1024 to 65535.
def test_index_returns_200():
request, response = SanicTestClient(app, port=None).get('/')
assert response.status == 200
2.21.2 pytest-sanic
pytest-sanic is a pytest plugin, it helps you to test your code asynchronously. Just write tests like,
50 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
pytest-sanic also provides some useful fixtures, like loop, unused_port, test_server, test_client.
@pytest.yield_fixture
def app():
app = Sanic("test_sanic_app")
@app.route("/test_get", methods=['GET'])
async def test_get(request):
return response.json({"GET": True})
@app.route("/test_post", methods=['POST'])
async def test_post(request):
return response.json({"POST": True})
yield app
@pytest.fixture
def test_cli(loop, app, test_client):
return loop.run_until_complete(test_client(app, protocol=WebSocketProtocol))
#########
# Tests #
#########
2.21. Testing 51
Sanic Documentation, Release 19.12.2
2.22 Deploying
Deploying Sanic is very simple using one of three options: the inbuilt webserver, an ASGI webserver, or gunicorn. It
is also very common to place Sanic behind a reverse proxy, like nginx.
After defining an instance of sanic.Sanic, we can call the run method with the following keyword arguments:
• host (default `”127.0.0.1”`): Address to host the server on.
• port (default `8000`): Port to host the server on.
• debug (default `False`): Enables debug output (slows server).
• ssl (default `None`): SSLContext for SSL encryption of worker(s).
• sock (default `None`): Socket for the server to accept connections from.
• workers (default `1`): Number of worker processes to spawn.
• loop (default `None`): An asyncio-compatible event loop. If none is specified, Sanic creates its own event loop.
• protocol (default `HttpProtocol`): Subclass of asyncio.protocol.
• access_log (default `True`): Enables log on handling requests (significantly slows server).
In the above example, we decided to turn off the access log in order to increase performance.
Workers
By default, Sanic listens in the main process using only one CPU core. To crank up the juice, just specify the number
of workers in the run arguments.
Sanic will automatically spin up multiple processes and route traffic between them. We recommend as many workers
as you have available cores.
If you like using command line arguments, you can launch a Sanic webserver by executing the module. For example,
if you initialized Sanic as app in a file named server.py, you could run the server like so:
With this way of running sanic, it is not necessary to invoke app.run in your Python file. If you do, make sure you
wrap it so that it only executes when directly run by the interpreter.
if __name__ == '__main__':
app.run(host='0.0.0.0', port=1337, workers=4)
52 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
Sanic is also ASGI-compliant. This means you can use your preferred ASGI webserver to run Sanic. The three main
implementations of ASGI are Daphne, Uvicorn, and Hypercorn.
Follow their documentation for the proper way to run them, but it should look something like:
daphne myapp:app
uvicorn myapp:app
hypercorn myapp:app
Gunicorn ‘Green Unicorn’ is a WSGI HTTP Server for UNIX. It’s a pre-fork worker model ported from Ruby’s
Unicorn project.
In order to run Sanic application with Gunicorn, you need to use the special sanic.worker.GunicornWorker for Guni-
corn worker-class argument:
If your application suffers from memory leaks, you can configure Gunicorn to gracefully restart a worker after it has
processed a given number of requests. This can be a convenient way to help limit the effects of the memory leak.
See the Gunicorn Docs for more information.
Sanic can be used with a reverse proxy (e.g. nginx). There’s a simple example of nginx configuration:
server {
listen 80;
server_name example.org;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
If you want to get real client ip, you should configure X-Real-IP and X-Forwarded-For HTTP headers and set
app.config.PROXIES_COUNT to 1; see the configuration page for more information.
2.22. Deploying 53
Sanic Documentation, Release 19.12.2
To improve the performance add debug=False and access_log=False in the run arguments.
app.config.ACCESS_LOG = False
This is suitable if you need to share the Sanic process with other applications, in particular the loop. However, be
advised that this method does not support using multiple processes, and is not the preferred way to run the app in
general.
Here is an incomplete example (please see run_async.py in examples for something more practical):
Caveat: using this method, calling app.create_server() will trigger “before_server_start” server events, but not “af-
ter_server_start”, “before_server_stop”, or “after_server_stop” server events.
For more advanced use-cases, you can trigger these events using the AsyncioServer object, returned by awaiting the
server task.
Here is an incomplete example (please see run_async_advanced.py in examples for something more complete):
54 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
2.23 Extensions
2.24 Examples
This section of the documentation is a simple collection of example code that can help you get a quick start on your
application development. Most of these examples are categorized and provide you with a link to the working code
example in the Sanic Repository
This section of the examples are a collection of code that provide a simple use case example of the sanic application.
Simple Apps
A simple sanic application with a single async method with text and json type response.
app = Sanic(__name__)
@app.route("/")
async def test(req):
return res.text("I\'m a teapot", status=418)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000)
app = Sanic(__name__)
@app.route("/")
async def test(request):
return response.json({"test": True})
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000)
2.23. Extensions 55
Sanic Documentation, Release 19.12.2
Showcasing the simple mechanism of using sanic.views.HTTPMethodView as well as a way to extend the
same into providing a custom async behavior for view.
app = Sanic('some_name')
class SimpleView(HTTPMethodView):
class SimpleAsyncView(HTTPMethodView):
app.add_route(SimpleView.as_view(), '/')
app.add_route(SimpleAsyncView.as_view(), '/async')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000, debug=True)
56 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
URL Redirect
app = Sanic(__name__)
@app.route('/')
def handle_request(request):
return response.redirect('/redirect')
@app.route('/redirect')
async def test(request):
return response.json({"Redirected": True})
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000)
Sanic provides an easy to use way of redirecting the requests via a helper method called url_for that takes a
unique url name as argument and returns you the actual route assigned for it. This will help in simplifying the efforts
required in redirecting the user between different section of the application.
app = Sanic(__name__)
@app.route('/')
async def index(request):
# generate a URL for the endpoint `post_handler`
url = app.url_for('post_handler', post_id=5)
# the URL is `/posts/5`, redirect to it
return response.redirect(url)
@app.route('/posts/<post_id>')
async def post_handler(request, post_id):
return response.text('Post - {}'.format(post_id))
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000, debug=True)
2.24. Examples 57
Sanic Documentation, Release 19.12.2
Blueprints
Sanic provides an amazing feature to group your APIs and routes under a logical collection that can easily be
imported and plugged into any of your sanic application and it’s called blueprints
app = Sanic(__name__)
blueprint = Blueprint('name', url_prefix='/my_blueprint')
blueprint2 = Blueprint('name2', url_prefix='/my_blueprint2')
blueprint3 = Blueprint('name3', url_prefix='/my_blueprint3')
@blueprint.route('/foo')
async def foo(request):
return json({'msg': 'hi from blueprint'})
@blueprint2.route('/foo')
async def foo2(request):
return json({'msg': 'hi from blueprint2'})
@blueprint3.route('/foo')
async def index(request):
return await file('websocket.html')
@app.websocket('/feed')
async def foo3(request, ws):
while True:
data = 'hello!'
print('Sending: ' + data)
await ws.send(data)
data = await ws.recv()
print('Received: ' + data)
app.blueprint(blueprint)
app.blueprint(blueprint2)
app.blueprint(blueprint3)
Logging Enhancements
Even though Sanic comes with a battery of Logging support it allows the end users to customize the way logging is
handled in the application runtime.
58 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
logging.basicConfig(
format=logging_format,
level=logging.DEBUG
)
log = logging.getLogger()
@sanic.route("/")
def test(request):
log.info("received request; responding with 'hey'")
return response.text("hey")
sanic.run(host="0.0.0.0", port=8000)
The following sample provides an example code that demonstrates the usage of sanic.app.Sanic.
middleware() in order to provide a mechanism to assign a unique request ID for each of the incoming requests
and log them via aiotask-context.
'''
Based on example from https://github.com/Skyscanner/aiotask-context
and `examples/{override_logging,run_async}.py`.
Needs https://github.com/Skyscanner/aiotask-context/tree/
˓→52efbc21e2e1def2d52abb9a8e951f3ce5e6f690 or newer
import asyncio
import uuid
import logging
from signal import signal, SIGINT
import uvloop
import aiotask_context as context
log = logging.getLogger(__name__)
class RequestIdFilter(logging.Filter):
def filter(self, record):
record.request_id = context.get('X-Request-ID')
return True
LOG_SETTINGS = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
(continues on next page)
2.24. Examples 59
Sanic Documentation, Release 19.12.2
},
},
'loggers': {
'': {
'level': 'DEBUG',
'handlers': ['console'],
'propagate': True
},
}
}
@app.middleware('request')
async def set_request_id(request):
request_id = request.headers.get('X-Request-ID') or str(uuid.uuid4())
context.set("X-Request-ID", request_id)
@app.route("/")
async def test(request):
log.debug('X-Request-ID: %s', context.get('X-Request-ID'))
log.info('Hello from test!')
return response.json({"test": True})
if __name__ == '__main__':
asyncio.set_event_loop(uvloop.new_event_loop())
server = app.create_server(host="0.0.0.0", port=8000, return_asyncio_server=True)
loop = asyncio.get_event_loop()
loop.set_task_factory(context.task_factory)
task = asyncio.ensure_future(server)
try:
loop.run_forever()
except:
loop.stop()
60 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
Sanic framework comes with in-built support for streaming large files and the following code explains the process
to setup a Sanic application with streaming support.
from sanic import Sanic
from sanic.views import CompositionView
from sanic.views import HTTPMethodView
from sanic.views import stream as stream_decorator
from sanic.blueprints import Blueprint
from sanic.response import stream, text
bp = Blueprint('blueprint_request_stream')
app = Sanic('request_stream')
class SimpleView(HTTPMethodView):
@stream_decorator
async def post(self, request):
result = ''
while True:
body = await request.stream.get()
if body is None:
break
result += body.decode('utf-8')
return text(result)
@app.post('/stream', stream=True)
async def handler(request):
async def streaming(response):
while True:
body = await request.stream.get()
if body is None:
break
body = body.decode('utf-8').replace('1', 'A')
await response.write(body)
return stream(streaming)
@bp.put('/bp_stream', stream=True)
async def bp_handler(request):
result = ''
while True:
body = await request.stream.get()
if body is None:
break
result += body.decode('utf-8').replace('1', 'A')
return text(result)
2.24. Examples 61
Sanic Documentation, Release 19.12.2
app.blueprint(bp)
app.add_route(SimpleView.as_view(), '/method_view')
view = CompositionView()
view.add(['POST'], post_handler, stream=True)
app.add_route(view, '/composition_view')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)
Sample Client app to show the usage of streaming application by a client code.
import requests
data = ""
for i in range(1, 250000):
data += str(i)
r = requests.post('http://0.0.0.0:8000/stream', data=data)
print(r.text)
Sanic supports the ability to start an app with multiple worker support. However, it’s important to be able to limit
the concurrency per process/loop in order to ensure an efficient execution. The following section of the code provides
a brief example of how to limit the concurrency with the help of asyncio.Semaphore
from sanic import Sanic
from sanic.response import json
import asyncio
import aiohttp
app = Sanic(__name__)
sem = None
@app.listener('before_server_start')
def init(sanic, loop):
global sem
concurrency_per_worker = 4
sem = asyncio.Semaphore(concurrency_per_worker, loop=loop)
62 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
@app.route("/")
async def test(request):
"""
Download and serve example JSON
"""
url = "https://api.github.com/repos/channelcat/sanic"
Deploying a sanic app via docker and docker-compose is an easy task to achieve and the following example
provides a deployment of the sample simple_server.py
FROM python:3.5
MAINTAINER Channel Cat <channelcat@gmail.com>
ADD . /code
RUN pip3 install git+https://github.com/channelcat/sanic
EXPOSE 8000
WORKDIR /code
version: '2'
services:
sanic:
build: .
ports:
- "8000:8000"
Sanic provides an extendable bare minimum implementation of a global exception handler via sanic.handlers.
ErrorHandler. This example shows how to extend it to enable some custom behaviors.
"""
Example intercepting uncaught exceptions using Sanic's error handler framework.
This may be useful for developers wishing to use Sentry, Airbrake, etc.
or a custom system to log and monitor unexpected errors in production.
First we create our own class inheriting from Handler in sanic.exceptions,
and pass in an instance of it when we create our Sanic instance. Inside this
class' default handler, we can do anything including sending exceptions to
an external service.
(continues on next page)
2.24. Examples 63
Sanic Documentation, Release 19.12.2
class CustomHandler(ErrorHandler):
# Some exceptions are trivial and built into Sanic (404s, etc)
if not isinstance(exception, SanicException):
print(exception)
"""
This is an ordinary Sanic server, with the exception that we set the
server's error_handler to an instance of our CustomHandler
"""
app = Sanic(__name__)
handler = CustomHandler()
app.error_handler = handler
@app.route("/")
async def test(request):
# Here, something occurs which causes an unexpected exception
# This exception will flow to our custom handler.
raise SanicException('You Broke It!')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000, debug=True)
64 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
• LogDNA
import logging
import socket
from os import getenv
from platform import node
from uuid import getnode as get_mac
log = logging.getLogger('logdna')
log.setLevel(logging.INFO)
def get_my_ip_address(remote_server="google.com"):
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect((remote_server, 80))
return s.getsockname()[0]
def get_mac_address():
h = iter(hex(get_mac())[2:].zfill(12))
return ":".join(i + next(h) for i in h)
logdna_options = {
"app": __name__,
"index_meta": True,
"hostname": node(),
"ip": get_my_ip_address(),
"mac": get_mac_address()
}
logdna = logging.getLogger(__name__)
logdna.setLevel(logging.INFO)
logdna.addHandler(logdna_handler)
app = Sanic(__name__)
@app.middleware
def log_request(request: Request):
logdna.info("I was Here with a new Request to URL: {}".format(request.url))
@app.route("/")
def default(request):
return json({
"response": "I was here"
})
(continues on next page)
2.24. Examples 65
Sanic Documentation, Release 19.12.2
if __name__ == "__main__":
app.run(
host="0.0.0.0",
port=getenv("PORT", 8080)
)
• RayGun
from os import getenv
class RaygunExceptionReporter(ErrorHandler):
self.sender = RaygunSender(raygun_api_key)
raygun_error_reporter = RaygunExceptionReporter()
app = Sanic(__name__, error_handler=raygun_error_reporter)
@app.route("/raise")
async def test(request):
raise SanicException('You Broke It!')
if __name__ == '__main__':
app.run(
host="0.0.0.0",
port=getenv("PORT", 8080)
)
• Rollbar
import rollbar
66 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
class RollbarExceptionHandler(ErrorHandler):
@app.route("/raise")
def create_error(request):
raise SanicException("I was here and I don't like where I am")
if __name__ == "__main__":
app.run(
host="0.0.0.0",
port=getenv("PORT", 8080)
)
• Sentry
from os import getenv
sentry_init(
dsn=getenv("SENTRY_DSN"),
integrations=[SanicIntegration()],
)
app = Sanic(__name__)
# noinspection PyUnusedLocal
@app.route("/working")
async def working_path(request):
return json({
"response": "Working API Response"
})
# noinspection PyUnusedLocal
@app.route("/raise-error")
async def raise_error(request):
raise Exception("Testing Sentry Integration")
if __name__ == '__main__':
(continues on next page)
2.24. Examples 67
Sanic Documentation, Release 19.12.2
Security
The following sample code shows a simple decorator based authentication and authorization mechanism that can be
setup to secure your sanic api endpoints.
app = Sanic()
def check_request_for_authorization_status(request):
# Note: Define your check, for instance cookie, session.
flag = True
return flag
def authorized(f):
@wraps(f)
async def decorated_function(request, *args, **kwargs):
# run some method that checks the request
# for the client's authorization status
is_authorized = check_request_for_authorization_status(request)
if is_authorized:
# the user is authorized.
# run the handler method and return the response
response = await f(request, *args, **kwargs)
return response
else:
# the user is not authorized.
return json({'status': 'not_authorized'}, 403)
return decorated_function
@app.route("/")
@authorized
async def test(request):
return json({'status': 'authorized'})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
68 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
Sanic Websocket
Sanic provides an ability to easily add a route and map it to a websocket handlers.
<!DOCTYPE html>
<html>
<head>
<title>WebSocket demo</title>
</head>
<body>
<script>
var ws = new WebSocket('ws://' + document.domain + ':' + location.port +
˓→'/feed'),
messages = document.createElement('ul');
ws.onmessage = function (event) {
var messages = document.getElementsByTagName('ul')[0],
message = document.createElement('li'),
content = document.createTextNode('Received: ' + event.data);
message.appendChild(content);
messages.appendChild(message);
};
document.body.appendChild(messages);
window.setInterval(function() {
data = 'bye!'
ws.send(data);
var messages = document.getElementsByTagName('ul')[0],
message = document.createElement('li'),
content = document.createTextNode('Sent: ' + data);
message.appendChild(content);
messages.appendChild(message);
}, 1000);
</script>
</body>
</html>
app = Sanic(__name__)
@app.route('/')
async def index(request):
return await file('websocket.html')
@app.websocket('/feed')
async def feed(request, ws):
while True:
data = 'hello!'
print('Sending: ' + data)
await ws.send(data)
data = await ws.recv()
print('Received: ' + data)
if __name__ == '__main__':
(continues on next page)
2.24. Examples 69
Sanic Documentation, Release 19.12.2
vhost Suppport
# Usage
# curl -H "Host: example.com" localhost:8000
# curl -H "Host: sub.example.com" localhost:8000
# curl -H "Host: bp.example.com" localhost:8000/question
# curl -H "Host: bp.example.com" localhost:8000/answer
app = Sanic()
bp = Blueprint("bp", host="bp.example.com")
@app.route('/', host=["example.com",
"somethingelse.com",
"therestofyourdomains.com"])
async def hello(request):
return response.text("Some defaults")
@app.route('/', host="sub.example.com")
async def hello(request):
return response.text("42")
@bp.route("/question")
async def hello(request):
return response.text("What is the meaning of life?")
@bp.route("/answer")
async def hello(request):
return response.text("42")
app.blueprint(bp)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000)
70 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
The following example shows you how to get up and running with unit testing sanic application with parallel test
execution support provided by the pytest-xdist plugin.
@pytest.fixture(scope="session")
def test_port(worker_id):
m = re.search(r'[0-9]+', worker_id)
if m:
num_id = m.group(0)
else:
num_id = 0
port = PORT_BASE + int(num_id)
return port
@pytest.fixture(scope="session")
def app():
app = Sanic()
@app.route('/')
async def index(request):
return text('OK')
return app
@pytest.fixture(scope="session")
def client(app, test_port):
return SanicTestClient(app, test_port)
@pytest.mark.parametrize('run_id', range(100))
def test_index(client, run_id):
request, response = client._sanic_endpoint_test('get', '/')
assert response.status == 200
assert response.text == 'OK'
2.24. Examples 71
Sanic Documentation, Release 19.12.2
The request object in Sanic is a kind of dict object, this means that request object can be manipulated as a
regular dict object.
from sanic import Sanic
from sanic.response import text
from random import randint
app = Sanic()
@app.middleware('request')
def append_request(request):
# Add new key with random value
request['num'] = randint(0, 100)
@app.get('/pop')
def pop_handler(request):
# Pop key from request object
num = request.pop('num')
return text(num)
@app.get('/key_exist')
def key_exist_handler(request):
# Check the key is exist or not
if 'num' in request:
return text('num exist in request')
For more examples and useful samples please visit the Huge-Sanic’s GitHub Page
2.25 Changelog
Bugfixes
72 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
– If you define a middleware via @app.middleware then it will be applied on all available routes (#37)
• Fix url_for behavior with missing SERVER_NAME
If the SERVER_NAME was missing in the app.config entity, the url_for on the request and app were failing due
to an AttributeError. This fix makes the availability of SERVER_NAME on our app.config an optional behavior.
(#1707)
Improved Documentation
Features
Improved Documentation
Features
• #1562 Remove aiohttp dependencey and create new SanicTestClient based upon requests-async
• #1475 Added ASGI support (Beta)
• #1436 Add Configure support from object string
2.25. Changelog 73
Sanic Documentation, Release 19.12.2
Bugfixes
Warning: Sanic will not support Python 3.5 from version 19.6 and forward. However, version 18.12LTS will
have its support period extended thru December 2020, and therefore passing Python’s official support version 3.5,
which is set to expire in September 2020.
Features
• #1497 Add support for zero-length and RFC 5987 encoded filename for multipart/form-data requests.
• #1484 The type of expires attribute of sanic.cookies.Cookie is now enforced to be of type
datetime.
• #1482 Add support for the stream parameter of sanic.Sanic.add_route() available to sanic.
Blueprint.add_route().
• #1481 Accept negative values for route parameters with type int or number.
• #1476 Deprecated the use of sanic.request.Request.raw_args - it has a fundamental flaw in which
is drops repeated query string parameters. Added sanic.request.Request.query_args as a replace-
ment for the original use-case.
• #1472 Remove an unwanted None check in Request class repr implementation. This changes the default
repr of a Request from <Request> to <Request: None />
• #1470 Added 2 new parameters to sanic.app.Sanic.create_server:
– return_asyncio_server - whether to return an asyncio.Server.
– asyncio_server_kwargs - kwargs to pass to loop.create_server for the event loop that sanic
is using.
This is a breaking change.
• #1499 Added a set of test cases that test and benchmark route resolution.
• #1457 The type of the "max-age" value in a sanic.cookies.Cookie is now enforced to be an integer.
Non-integer values are replaced with 0.
• #1445 Added the endpoint attribute to an incoming request, containing the name of the handler function.
74 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
• #1423 Improved request streaming. request.stream is now a bounded-size buffer instead of an un-
bounded queue. Callers must now call await request.stream.read() instead of await request.
stream.get() to read each portion of the body.
This is a breaking change.
Bugfixes
• #1502 Sanic was prefetching time.time() and updating it once per second to avoid excessive time.
time() calls. The implementation was observed to cause memory leaks in some cases. The benefit of the
prefetch appeared to negligible, so this has been removed. Fixes #1500
• #1501 Fix a bug in the auto-reloader when the process was launched as a module i.e. python -m init0.
mod1 where the sanic server is started in init0/mod1.py with debug enabled and imports another module
in init0.
• #1376 Allow sanic test client to bind to a random port by specifying port=None when constructing a
SanicTestClient
• #1399 Added the ability to specify middleware on a blueprint group, so that all routes produced from the
blueprints in the group have the middleware applied.
• #1442 Allow the the use the SANIC_ACCESS_LOG environment variable to enable/disable the access log when
not explicitly passed to app.run(). This allows the access log to be disabled for example when running via
gunicorn.
Developer infrastructure
Improved Documentation
2.25. Changelog 75
Sanic Documentation, Release 19.12.2
18.12.0
• Changes:
– Improved codebase test coverage from 81% to 91%.
– Added stream_large_files and host examples in static_file document
– Added methods to append and finish body content on Request (#1379)
– Integrated with .appveyor.yml for windows ci support
– Added documentation for AF_INET6 and AF_UNIX socket usage
– Adopt black/isort for codestyle
– Cancel task when connection_lost
– Simplify request ip and port retrieval logic
– Handle config error in load config file.
– Integrate with codecov for CI
– Add missed documentation for config section.
– Deprecate Handler.log
– Pinned httptools requirement to version 0.0.10+
• Fixes:
– Fix remove_entity_headers helper function (#1415)
– Fix TypeError when use Blueprint.group() to group blueprint with default url_prefix, Use os.path.normpath
to avoid invalid url_prefix like api//v1 f8a6af1 Rename the http module to helpers to prevent conflicts
with the built-in Python http library (fixes #1323)
– Fix unittests on windows
– Fix Namespacing of sanic logger
– Fix missing quotes in decorator example
– Fix redirect with quoted param
– Fix doc for latest blueprint code
– Fix build of latex documentation relating to markdown lists
– Fix loop exception handling in app.py
– Fix content length mismatch in windows and other platform
– Fix Range header handling for static files (#1402)
– Fix the logger and make it work (#1397)
76 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
0.8.3
• Changes:
– Ownership changed to org ‘huge-success’
0.8.0
• Changes:
– Add Server-Sent Events extension (Innokenty Lebedev)
– Graceful handling of request_handler_task cancellation (Ashley Sommer)
– Sanitize URL before redirection (aveao)
– Add url_bytes to request (johndoe46)
– py37 support for travisci (yunstanford)
– Auto reloader support for OSX (garyo)
– Add UUID route support (Volodymyr Maksymiv)
– Add pausable response streams (Ashley Sommer)
– Add weakref to request slots (vopankov)
– remove ubuntu 12.04 from test fixture due to deprecation (yunstanford)
– Allow streaming handlers in add_route (kinware)
– use travis_retry for tox (Raphael Deem)
– update aiohttp version for test client (yunstanford)
– add redirect import for clarity (yingshaoxo)
– Update HTTP Entity headers (Arnulfo Solís)
– Add register_listener method (Stephan Fitzpatrick)
– Remove uvloop/ujson dependencies for Windows (abuckenheimer)
– Content-length header on 204/304 responses (Arnulfo Solís)
– Extend WebSocketProtocol arguments and add docs (Bob Olde Hampsink, yunstanford)
– Update development status from pre-alpha to beta (Maksim Anisenkov)
– KeepAlive Timout log level changed to debug (Arnulfo Solís)
2.25. Changelog 77
Sanic Documentation, Release 19.12.2
78 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
0.1.7
0.1.6
• Static files
• Lazy Cookie Loading
0.1.5
• Cookies
• Blueprint listeners and ordering
• Faster Router
• Fix: Incomplete file reads on medium+ sized post requests
• Breaking: after_start and before_stop now pass sanic as their first argument
0.1.4
• Multiprocessing
0.1.3
• Blueprint support
• Faster Response processing
2.25. Changelog 79
Sanic Documentation, Release 19.12.2
0.1.1 - 0.1.2
0.1.0
• Released to public
2.26 Contributing
Thank you for your interest! Sanic is always looking for contributors. If you don’t feel comfortable contributing code,
adding docstrings to the source files is very appreciated.
We are committed to providing a friendly, safe and welcoming environment for all, regardless of gender, sexual
orientation, disability, ethnicity, religion, or similar personal characteristic. Our code of conduct sets the standards for
behavior.
2.26.1 Installation
To develop on sanic (and mainly to just run the tests) it is highly recommend to install from sources.
So assume you have already cloned the repo and are in the working directory with a virtual environment already set
up, then run:
Sanic doesn’t use requirements*.txt files to manage any kind of dependencies related to it in order to simplify
the effort required in managing the dependencies. Please make sure you have read and understood the following section
of the document that explains the way sanic manages dependencies inside the setup.py file.
80 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
To run the tests for Sanic it is recommended to use tox like so:
tox
tox -e lint
tox -e check
tox -e security
2.26. Contributing 81
Sanic Documentation, Release 19.12.2
tox -e docs
isort
isort sorts Python imports. It divides imports into three categories sorted each in alphabetical order.
1. built-in
2. third-party
3. project-specific
black
flake8
flake8 is a Python style guide that wraps following tools into one.
1. PyFlakes
2. pycodestyle
3. Ned Batchelder’s McCabe script
isort, black and flake8 checks are performed during tox lint checks.
Refer tox documentation for more details.
82 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
2.26.11 Changelog
It is mandatory to add documentation for Change log as part of your Pull request when you fix/contribute something
to the sanic community. This will enable us in generating better and well defined change logs during the release
which can aid community users in a great way.
2.26.12 Documentation
Sanic’s documentation is built using sphinx. Guides are written in Markdown and can be found in the docs folder,
while the module reference is automatically generated using sphinx-apidoc.
To generate the documentation from scratch:
# There is a simple make command provided to ease the work required in generating
# the documentation
make docs
2.26. Contributing 83
Sanic Documentation, Release 19.12.2
Warning: One of the main goals of Sanic is speed. Code that lowers the performance of Sanic without significant
gains in usability, security, or features may not be merged. Please don’t let this intimidate you! If you have any
concerns about an idea, open an issue for discussion and help.
2.27.1 Submodules
84 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
• name – A unique name assigned to the URL so that it can be used with url_for()
Returns Objected decorated by websocket()
property asgi_client
blueprint(blueprint, **options)
Register a blueprint on the application.
Parameters
• blueprint – Blueprint object or (list, tuple) thereof
• options – option dictionary with blueprint defaults
Returns Nothing
converted_response_type(response)
No implementation provided.
async create_server(host: Optional[str] = None, port: Optional[int] = None, debug: bool
= False, ssl: Union[dict, ssl.SSLContext, None] = None, sock: Op-
tional[socket.socket] = None, protocol: Type[asyncio.protocols.Protocol]
= None, backlog: int = 100, stop_event: Any = None, ac-
cess_log: Optional[bool] = None, return_asyncio_server=False, asyn-
cio_server_kwargs=None) → Optional[sanic.server.AsyncioServer]
Asynchronous version of run().
This method will take care of the operations necessary to invoke the before_start events via
trigger_events() method invocation before starting the sanic app in Async mode.
Note: This does not support multiprocessing and is not the preferred way to run a Sanic application.
Parameters
• host (str) – Address to host on
• port (int) – Port to host on
• debug (bool) – Enables debug output (slows server)
• ssl (SSLContext or dict) – SSLContext, or location of certificate and key for SSL
encryption of worker(s)
• sock (socket) – Socket for the server to accept connections from
• protocol (type[Protocol]) – Subclass of asyncio Protocol class
• backlog (int) – a number of unaccepted connections that the system will allow before
refusing new connections
• stop_event (None) – event to be triggered before stopping the app - deprecated
• access_log (bool) – Enables writing access logs (slows server)
• return_asyncio_server (bool) – flag that defines whether there’s a need to return
asyncio.Server or start it serving right away
• asyncio_server_kwargs (dict) – key-value arguments for asyncio/uvloop cre-
ate_server method
Returns AsyncioServer if return_asyncio_server is true, else Nothing
86 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
Parameters
• args – Blueprint object or (list, tuple) thereof
• kwargs – option dictionary with blueprint defaults
Returns None
register_listener(listener, event)
Register the listener for a given event.
Parameters
• listener – callable i.e. setup_db(app, loop)
• event – when to register listener i.e. ‘before_server_start’
Returns listener
register_middleware(middleware, attach_to='request')
Register an application level middleware that will be attached to all the API URLs registered under this
application.
This method is internally invoked by the middleware() decorator provided at the app level.
Parameters
• middleware – Callback method to be attached to the middleware
• attach_to – The state at which the middleware needs to be invoked in the lifecycle
of an HTTP Request. request - Invoke before the request is processed response - Invoke
before the response is returned back
Returns decorated method
register_named_middleware(middleware, route_names, attach_to='request')
remove_route(uri, clean_cache=True, host=None)
This method provides the app user a mechanism by which an already existing route can be removed from
the Sanic object
88 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
Warning: remove_route is deprecated in v19.06 and will be removed from future versions.
Parameters
• uri – URL Path to be removed from the app
• clean_cache – Instruct sanic if it needs to clean up the LRU route cache
• host – IP address or FQDN specific to the host
Returns None
Returns Nothing
static(uri, file_or_directory, pattern='/?.+', use_modified_since=True, use_content_range=False,
stream_large_files=False, name='static', host=None, strict_slashes=None, con-
tent_type=None)
Register a root to serve files from. The input can either be a file or a directory. This method will enable an
easy and simple way to setup the Route necessary to serve the static files.
Parameters
• uri – URL path to be used for serving static content
• file_or_directory – Path for the Static file/directory with static files
• pattern – Regex Pattern identifying the valid static files
• use_modified_since – If true, send file modified time, and return not modified if the
browser’s matches the server’s
• use_content_range – If true, process header for range requests and sends the file
part that is requested
• stream_large_files – If true, use the StreamingHTTPResponse.
file_stream() handler rather than the HTTPResponse.file() handler to
send the file. If this is an integer, this represents the threshold size to switch to
StreamingHTTPResponse.file_stream()
• name – user defined name used for url_for
• host – Host IP or FQDN for the service to use
• strict_slashes – Instruct Sanic to check if the request URLs need to terminate
with a /
• content_type – user defined content type for header
Returns None
stop()
This kills the Sanic
property test_client
async trigger_events(events, loop)
Trigger events (functions or async) :param events: one or more sync or async functions to execute :param
loop: event loop
url_for(view_name: str, **kwargs)
Build a URL based on a view name and the values provided.
In order to build a URL, all request parameters must be supplied as keyword arguments, and each parameter
must pass the test for the specified parameter type. If these conditions are not met, a URLBuildError will
be thrown.
Keyword arguments that are not request parameters will be included in the output URL’s query string.
Parameters
• view_name – string referencing the view name
• **kwargs – keys and values that are used to build request parameters and query string
arguments.
Returns the built URL
Raises: URLBuildError
90 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
:return a decorated method to handle global exceptions for any route registered under this blueprint.
92 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
:return a decorated method that when invoked will return an object of type FutureRoute
94 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
property name
Alias for field number 7
property stream
Alias for field number 5
property strict_slashes
Alias for field number 4
property uri
Alias for field number 1
property version
Alias for field number 6
class sanic.blueprints.FutureStatic(uri, file_or_directory, args, kwargs)
Bases: tuple
property args
Alias for field number 2
property file_or_directory
Alias for field number 1
property kwargs
Alias for field number 3
property uri
Alias for field number 0
class sanic.blueprint_group.BlueprintGroup(url_prefix=None)
Bases: collections.abc.MutableSequence
This class provides a mechanism to implement a Blueprint Group using the group() method in Blueprint.
To avoid having to re-write some of the existing implementation, this class provides a custom iterator imple-
mentation that will let you use the object of this class as a list/tuple inside the existing implementation.
property blueprints
Retrieve a list of all the available blueprints under this group. :return: List of Blueprint instance
insert(index: int, item: object) → None
The Abstract class MutableSequence leverages this insert method to perform the BlueprintGroup.append
operation.
Parameters
• index – Index to use for removing a new Blueprint item
• item – New Blueprint object.
Returns None
middleware(*args, **kwargs)
A decorator that can be used to implement a Middleware plugin to all of the Blueprints that belongs to this
specific Blueprint Group.
In case of nested Blueprint Groups, the same middleware is applied across each of the Blueprints recur-
sively.
Parameters
96 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
or also:
app.config.from_object('myproject.config.MyConfigClass')
You should not use this function to load the actual configuration but rather configuration defaults. The
actual config should be loaded with from_pyfile() and ideally from a location not within the package
because the package might be installed system wide.
Parameters obj – an object holding the configuration
from_pyfile(filename)
Update the values in the config from a Python file. Only the uppercase variables in that module are stored
in the config.
Parameters filename – an absolute path to the config file
load_environment_vars(prefix='SANIC_')
Looks for prefixed environment variables and applies them to the configuration if present.
sanic.config.strtobool(val)
This function was borrowed from distutils.utils. While distutils is part of stdlib, it feels odd to use distutils in
main application code.
The function was modified to walk its talk and actually return bool and not int.
98 Chapter 2. Guides
Sanic Documentation, Release 19.12.2
quiet = True
status_code = 400
exception sanic.exceptions.MethodNotSupported(message, method, allowed_methods)
Bases: sanic.exceptions.SanicException
quiet = True
status_code = 405
exception sanic.exceptions.NotFound(message, status_code=None, quiet=None)
Bases: sanic.exceptions.SanicException
quiet = True
status_code = 404
exception sanic.exceptions.PayloadTooLarge(message, status_code=None, quiet=None)
Bases: sanic.exceptions.SanicException
quiet = True
status_code = 413
exception sanic.exceptions.PyFileError(file)
Bases: Exception
exception sanic.exceptions.RequestTimeout(message, status_code=None, quiet=None)
Bases: sanic.exceptions.SanicException
The Web server (running the Web site) thinks that there has been too long an interval of time between 1) the
establishment of an IP connection (socket) between the client and the server and 2) the receipt of any data on
that socket, so the server has dropped the connection. The socket connection has actually been lost - the Web
server has ‘timed out’ on that particular socket connection.
quiet = True
status_code = 408
exception sanic.exceptions.SanicException(message, status_code=None, quiet=None)
Bases: Exception
exception sanic.exceptions.ServerError(message, status_code=None, quiet=None)
Bases: sanic.exceptions.SanicException
status_code = 500
exception sanic.exceptions.ServiceUnavailable(message, status_code=None,
quiet=None)
Bases: sanic.exceptions.SanicException
The server is currently unavailable (because it is overloaded or down for maintenance). Generally, this is a
temporary state.
quiet = True
status_code = 503
exception sanic.exceptions.URLBuildError(message, status_code=None, quiet=None)
Bases: sanic.exceptions.ServerError
exception sanic.exceptions.Unauthorized(message, status_code=None, scheme=None,
**kwargs)
Bases: sanic.exceptions.SanicException
Unauthorized exception (401 HTTP status code).
Parameters
• message – Message describing the exception.
• status_code – HTTP Status code.
• scheme – Name of the authentication scheme to be used.
When present, kwargs is used to complete the WWW-Authentication header.
Examples:
quiet = True
status_code = 401
sanic.exceptions.abort(status_code, message=None)
Raise an exception based on SanicException. Returns the HTTP response message appropriate for the given
status code, unless provided.
Parameters
• status_code – The HTTP status code to return.
• message – The HTTP response body. Defaults to the messages in response.py for the
given status code.
sanic.exceptions.add_status_code(code, quiet=None)
Decorator used for adding exceptions to SanicException.
url_for(view_name, **kwargs)
Same as sanic.Sanic.url_for(), but automatically determine scheme and netloc base on the re-
quest. Since this method is aiming to generate correct schema & netloc, _external is implied.
Parameters kwargs – takes same parameters as in sanic.Sanic.url_for()
Returns an absolute url to the given view
Return type str
version
class sanic.request.RequestParameters
Bases: dict
Hosts a dict with lists as values where get returns the first value of the list and getlist returns the whole shebang
get(name, default=None)
Return the first value, either the default or actual
getlist(name, default=None)
Return the entire list
class sanic.request.StreamBuffer(buffer_size=100)
Bases: object
property buffer_size
is_full()
async put(payload)
async read()
Stop reading when gets None
sanic.request.parse_multipart_form(body, boundary)
Parse a request body and returns fields and files
Parameters
• body – bytes request body
• boundary – bytes multipart boundary
Returns fields (RequestParameters), files (RequestParameters)
class sanic.response.BaseHTTPResponse
Bases: object
property cookies
get_headers(version='1.1', keep_alive=False, keep_alive_timeout=None, body=b'')
Deprecated since version 20.3:.
This function is not public API and will be removed.
class sanic.response.HTTPResponse(body=None, status=200, headers=None, con-
tent_type=None, body_bytes=b'')
Bases: sanic.response.BaseHTTPResponse
body
content_type
property cookies
headers
output(version='1.1', keep_alive=False, keep_alive_timeout=None)
status
class sanic.response.StreamingHTTPResponse(streaming_fn, status=200, headers=None,
content_type='text/plain; charset=utf-8',
chunked=True)
Bases: sanic.response.BaseHTTPResponse
chunked
content_type
get_headers(version='1.1', keep_alive=False, keep_alive_timeout=None)
Deprecated since version 20.3:.
This function is not public API and will be removed.
headers
protocol
status
async stream(version='1.1', keep_alive=False, keep_alive_timeout=None)
Streams headers, runs the streaming_fn callback that writes content to the response body, then finalizes the
response body.
streaming_fn
async write(data)
Writes a chunk of data to the streaming response.
Parameters data – str or bytes-ish data to be written.
sanic.response.empty(status=204, headers=None)
Returns an empty response to the client.
:param status Response code. :param headers Custom Headers.
async sanic.response.file(location, status=200, mime_type=None, headers=None, file-
name=None, _range=None)
Return a response object with file data.
Parameters
• location – Location of file on system.
• mime_type – Specific mime_type.
• headers – Custom Headers.
• filename – Override filename.
• _range –
async sanic.response.file_stream(location, status=200, chunk_size=4096, mime_type=None,
headers=None, filename=None, chunked=True,
_range=None)
Return a streaming response object with file data.
Parameters
• location – Location of file on system.
Example usage:
@app.route("/")
async def index(request):
async def streaming_fn(response):
await response.write('foo')
await response.write('bar')
Parameters
• streaming_fn – A coroutine accepts a response and writes content to that response.
• mime_type – Specific mime_type.
• headers – Custom Headers.
• chunked – Enable or disable chunked transfer-encoding
property pattern
Alias for field number 2
property uri
Alias for field number 5
exception sanic.router.RouteDoesNotExist
Bases: Exception
exception sanic.router.RouteExists
Bases: Exception
class sanic.router.Router
Bases: object
Router supports basic routing with parameters and method checks
Usage:
or
Parameters will be passed as keyword arguments to the request handling function. Provided parameters can also
have a type by appending :type to the <parameter>. Given parameter must be able to be type-casted to this. If
no type is provided, a string is expected. A regular expression can also be passed in as the type. The argument
given to the function will always be a string, independent of the type.
add(uri, methods, handler, host=None, strict_slashes=False, version=None, name=None)
Add a handler to the route list
Parameters
• uri – path to match
• methods – sequence of accepted method names. If none are provided, any method is
allowed
• handler – request handler function. When executed, it should provide a response object.
• strict_slashes – strict to trailing slash
• version – current version of the route or blueprint. See docs for further details.
Returns Nothing
static check_dynamic_route_exists(pattern, routes_to_check, parameters)
Check if a URL pattern exists in a list of routes provided based on the comparison of URL pattern and the
parameters.
Parameters
• pattern – URL parameter pattern
• routes_to_check – list of dynamic routes either hashable or unhashable routes.
• parameters – List of Parameter items
Returns Tuple of index and route if matching route exists else -1 for index and None for route
find_route_by_view_name
Find a route in the router based on the specified view name.
Parameters
• view_name – string of view name to search by
• kwargs – additional params, usually for static files
Returns tuple containing (uri, Route)
get(request)
Get a request handler based on the URL of the request, or raises an error
Parameters request – Request object
Returns handler, arguments, keyword arguments
get_supported_methods(url)
Get a list of supported methods for a url and optional host.
Parameters url – URL string (including host)
Returns frozenset of supported methods
is_stream_handler(request)
Handler for request is stream or not. :param request: Request object :return: bool
parameter_pattern = re.compile('<(.+?)>')
classmethod parse_parameter_string(parameter_string)
Parse a parameter string into its constituent name, type, and pattern
For example:
parse_parameter_string('<param_one:[A-z]>')` ->
('param_one', str, '[A-z]')
after_stop()
Trigger “after_server_stop” events
before_stop()
Trigger “before_server_stop” events
close()
connections
is_serving()
loop
serve_coro
serve_forever()
server
start_serving()
wait_closed()
class sanic.server.HttpProtocol(*, loop, app, request_handler, error_handler,
signal=<sanic.server.Signal object>, connec-
tions=None, request_timeout=60, response_timeout=60,
keep_alive_timeout=5, request_max_size=None, re-
quest_buffer_queue_size=100, request_class=None, ac-
cess_log=True, keep_alive=True, is_request_stream=False,
router=None, state=None, debug=False, **kwargs)
Bases: asyncio.protocols.Protocol
This class provides a basic HTTP implementation of the sanic framework.
access_log
app
bail_out(message, from_error=False)
In case if the transport pipes are closed and the sanic app encounters an error while writing data to the
transport pipe, we log the error with proper details.
Parameters
• message (str) – Error message to display
• from_error (bool) – If the bail out was invoked while handling an exception scenario.
Returns None
async body_append(body)
cleanup()
This is called when KeepAlive feature is used, it resets the connection in order for it to be able to handle
receiving another request on the same connection.
close()
Force close the connection.
close_if_idle()
Close the connection if a request is not being sent or received
Returns boolean - True if closed, false if staying open
connection_lost(exc)
Called when the connection is lost or closed.
The argument is an exception object or None (the latter meaning a regular EOF is received or the connec-
tion was aborted or closed).
connection_made(transport)
Called when a connection is made.
The argument is the transport representing the pipe connection. To receive data, wait for data_received()
calls. When the connection is closed, connection_lost() is called.
connections
data_received(data)
Called when some data is received.
The argument is a bytes object.
async drain()
error_handler
execute_request_handler()
Invoke the request handler defined by the sanic.app.Sanic.handle_request() method
Returns None
expect_handler()
Handler for Expect Header.
headers
is_request_stream
property keep_alive
Check if the connection needs to be kept alive based on the params attached to the _keep_alive attribute,
Signal.stopped and HttpProtocol.parser.should_keep_alive()
Returns True if connection is to be kept alive False else
keep_alive_timeout
keep_alive_timeout_callback()
Check if elapsed time since last response exceeds our configured maximum keep alive timeout value and
if so, close the transport pipe and let the response writer handle the error.
Returns None
log_response(response)
Helper method provided to enable the logging of responses in case if the HttpProtocol.
access_log is enabled.
Parameters response (sanic.response.HTTPResponse or sanic.response.
StreamingHTTPResponse) – Response generated for the current request
Returns None
loop
on_body(body)
on_header(name, value)
on_headers_complete()
on_message_complete()
on_url(url)
parser
pause_writing()
Called when the transport’s buffer goes over the high-water mark.
Pause and resume calls are paired – pause_writing() is called once when the buffer goes strictly over
the high-water mark (even if subsequent writes increases the buffer size even more), and eventually re-
sume_writing() is called once when the buffer size reaches the low-water mark.
Note that if the buffer size equals the high-water mark, pause_writing() is not called – it must go strictly
over. Conversely, resume_writing() is called when the buffer size is equal or lower than the low-water
mark. These end conditions are important to ensure that things go as expected when either mark is zero.
NOTE: This is the only Protocol callback that is not called through EventLoop.call_soon() – if it were,
it would have no effect when it’s most needed (when the app keeps writing without yielding until
pause_writing() is called).
async push_data(data)
request
request_buffer_queue_size
request_class
request_handler
request_max_size
request_timeout
request_timeout_callback()
response_timeout
response_timeout_callback()
resume_writing()
Called when the transport’s buffer drains below the low-water mark.
See pause_writing() for details.
router
signal
state
async stream_append()
async stream_response(response)
Streams a response to the client asynchronously. Attaches the transport to the response so the response
consumer can write to the response as needed.
transport
url
write_error(exception)
write_response(response)
Writes response content synchronously to the transport.
class sanic.server.Signal
Bases: object
stopped = False
sanic.server.serve(host, port, app, request_handler, error_handler, before_start=None, af-
ter_start=None, before_stop=None, after_stop=None, debug=False, re-
quest_timeout=60, response_timeout=60, keep_alive_timeout=5, ssl=None,
sock=None, request_max_size=None, request_buffer_queue_size=100,
reuse_port=False, loop=None, protocol=<class 'sanic.server.HttpProtocol'>,
backlog=100, register_sys_signals=True, run_multiple=False, run_async=False,
connections=None, signal=<sanic.server.Signal object>, request_class=None,
access_log=True, keep_alive=True, is_request_stream=False, router=None,
websocket_max_size=None, websocket_max_queue=None, web-
socket_read_limit=65536, websocket_write_limit=65536, state=None, grace-
ful_shutdown_timeout=15.0, asyncio_server_kwargs=None)
Start asynchronous HTTP Server on an individual process.
Parameters
• host – Address to host on
• port – Port to host on
• request_handler – Sanic request handler with middleware
• error_handler – Sanic error handler with middleware
• before_start – function to be executed before the server starts listening. Takes argu-
ments 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 re-
spected. 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
• debug – enables debug output (slows server)
• request_timeout – time in seconds
• response_timeout – time in seconds
• keep_alive_timeout – time in seconds
• ssl – SSLContext
• sock – Socket for the server to accept connections from
• request_max_size – size in bytes, None for no limit
• reuse_port – True for multiple workers
• loop – asyncio compatible event loop
• protocol – subclass of asyncio protocol class
• run_async – bool: Do not create a new event loop for the server, and return an Async-
Server object rather than running it
• request_class – Request class to use
• access_log – disable/enable access log
• websocket_max_size – enforces the maximum size for incoming messages in bytes.
• websocket_max_queue – sets the maximum length of the queue that holds incoming
messages.
• websocket_read_limit – sets the high-water limit of the buffer for incoming bytes,
the low-water limit is half the high-water limit.
• websocket_write_limit – sets the high-water limit of the buffer for outgoing bytes,
the low-water limit is a quarter of the high-water limit.
• is_request_stream – disable/enable Request.stream
• request_buffer_queue_size – streaming request buffer queue size
• router – Router object
• graceful_shutdown_timeout – How long take to Force close non-idle connection
• asyncio_server_kwargs – key-value args for asyncio/uvloop create_server method
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.trigger_events(events, loop)
Trigger event callbacks (functions or async)
Parameters
• events – one or more sync or async functions to execute
• loop – event loop
• stream_large_files – If true, use the file_stream() handler rather than the file() han-
dler to send the file If this is an integer, this represents the threshold size to switch to
file_stream()
• name – user defined name used for url_for
• content_type – user defined content type for header
class sanic.views.CompositionView
Bases: object
Simple method-function mapped view for the sanic. You can add handler functions to methods (get, post, put,
patch, delete) for every HTTP method you want to support.
For example: view = CompositionView() view.add([‘GET’], lambda request: text(‘I am get method’))
view.add([‘POST’, ‘PUT’], lambda request: text(‘I am post/put method’))
etc.
If someone tries to use a non-implemented method, there will be a 405 response.
add(methods, handler, stream=False)
class sanic.views.HTTPMethodView
Bases: object
Simple class based implementation of view for the sanic. You should implement methods (get, post, put, patch,
delete) for the class to every HTTP method you want to support.
For example:
class DummyView(HTTPMethodView):
def get(self, request, *args, **kwargs):
return text('I am get method')
def put(self, request, *args, **kwargs):
return text('I am put method')
etc.
If someone tries to use a non-implemented method, there will be a 405 response.
If you need any url params just mention them in method definition:
class DummyView(HTTPMethodView):
def get(self, request, my_param_here, *args, **kwargs):
return text('I am get method with %s' % my_param_here)
router
signal
state
transport
url
async websocket_handshake(request, subprotocols=None)
write_response(response)
Writes response content synchronously to the transport.
class sanic.websocket.WebSocketConnection(send: Callable[MutableMapping[str,
Any], Awaitable[None]], receive:
Callable[Awaitable[MutableMapping[str,
Any]]])
Bases: object
async accept() → None
async close() → None
async receive(*args, **kwargs) → Optional[str]
async recv(*args, **kwargs) → Optional[str]
async send(data: Union[str, bytes], *args, **kwargs) → None
add_task(task)
Schedule a task to run later, after the loop has started. Different from asyncio.ensure_future in that it does
not also return a future, and the actual ensure_future call is delayed until before server start.
Parameters task – future, couroutine or awaitable
add_websocket_route(handler, uri, host=None, strict_slashes=None, subprotocols=None,
name=None)
A helper method to register a function as a websocket route.
Parameters
• handler – a callable function or instance of a class that can handle the websocket request
• host – Host IP or FQDN details
• uri – URL path that will be mapped to the websocket handler handler
• strict_slashes – If the API endpoint needs to terminate with a “/” or not
• subprotocols – Subprotocols to be used with websocket handshake
• name – A unique name assigned to the URL so that it can be used with url_for()
Returns Objected decorated by websocket()
property asgi_client
blueprint(blueprint, **options)
Register a blueprint on the application.
Parameters
• blueprint – Blueprint object or (list, tuple) thereof
• options – option dictionary with blueprint defaults
Returns Nothing
converted_response_type(response)
No implementation provided.
async create_server(host: Optional[str] = None, port: Optional[int] = None, debug: bool
= False, ssl: Union[dict, ssl.SSLContext, None] = None, sock: Op-
tional[socket.socket] = None, protocol: Type[asyncio.protocols.Protocol]
= None, backlog: int = 100, stop_event: Any = None, ac-
cess_log: Optional[bool] = None, return_asyncio_server=False, asyn-
cio_server_kwargs=None) → Optional[sanic.server.AsyncioServer]
Asynchronous version of run().
This method will take care of the operations necessary to invoke the before_start events via
trigger_events() method invocation before starting the sanic app in Async mode.
Note: This does not support multiprocessing and is not the preferred way to run a Sanic application.
Parameters
• host (str) – Address to host on
• port (int) – Port to host on
• debug (bool) – Enables debug output (slows server)
• ssl (SSLContext or dict) – SSLContext, or location of certificate and key for SSL
encryption of worker(s)
• sock (socket) – Socket for the server to accept connections from
• protocol (type[Protocol]) – Subclass of asyncio Protocol class
• backlog (int) – a number of unaccepted connections that the system will allow before
refusing new connections
• stop_event (None) – event to be triggered before stopping the app - deprecated
• access_log (bool) – Enables writing access logs (slows server)
• return_asyncio_server (bool) – flag that defines whether there’s a need to return
asyncio.Server or start it serving right away
• asyncio_server_kwargs (dict) – key-value arguments for asyncio/uvloop cre-
ate_server method
Returns AsyncioServer if return_asyncio_server is true, else Nothing
Parameters
• args – Blueprint object or (list, tuple) thereof
• kwargs – option dictionary with blueprint defaults
Returns None
register_listener(listener, event)
Register the listener for a given event.
Parameters
• listener – callable i.e. setup_db(app, loop)
• event – when to register listener i.e. ‘before_server_start’
Returns listener
register_middleware(middleware, attach_to='request')
Register an application level middleware that will be attached to all the API URLs registered under this
application.
This method is internally invoked by the middleware() decorator provided at the app level.
Parameters
• middleware – Callback method to be attached to the middleware
• attach_to – The state at which the middleware needs to be invoked in the lifecycle
of an HTTP Request. request - Invoke before the request is processed response - Invoke
before the response is returned back
Returns decorated method
register_named_middleware(middleware, route_names, attach_to='request')
remove_route(uri, clean_cache=True, host=None)
This method provides the app user a mechanism by which an already existing route can be removed from
the Sanic object
Warning: remove_route is deprecated in v19.06 and will be removed from future versions.
Parameters
• uri – URL Path to be removed from the app
• clean_cache – Instruct sanic if it needs to clean up the LRU route cache
• host – IP address or FQDN specific to the host
Returns None
Raises: URLBuildError
:return a decorated method to handle global exceptions for any route registered under this blueprint.
:return a decorated method that when invoked will return an object of type FutureRoute
With Python 3.7 AsyncIO got major update for the following types:
• asyncio.AbstractEventLoop
• asyncio.AbstractServer
This example shows how to use sanic with Python 3.7, to be precise: how to retrieve an asyncio server instance:
import asyncio
import socket
import os
app = Sanic(__name__)
@app.route("/")
async def test(request):
return json({"hello": "world"})
server_socket = '/tmp/sanic.sock'
if __name__ == "__main__":
loop = asyncio.get_event_loop()
srv_coro = app.create_server(
sock=sock,
return_asyncio_server=True,
asyncio_server_kwargs=dict(
start_serving=False
)
)
srv = loop.run_until_complete(srv_coro)
try:
assert srv.is_serving() is False
loop.run_until_complete(srv.start_serving())
assert srv.is_serving() is True
loop.run_until_complete(srv.serve_forever())
except KeyboardInterrupt:
srv.close()
loop.close()
Please note that uvloop does not support these features yet.
THREE
MODULE DOCUMENTATION
• genindex
• modindex
• search
133
Sanic Documentation, Release 19.12.2
s
sanic, 120
sanic.app, 84
sanic.blueprint_group, 96
sanic.blueprints, 91
sanic.config, 97
sanic.constants, 98
sanic.cookies, 98
sanic.exceptions, 98
sanic.handlers, 101
sanic.log, 102
sanic.request, 102
sanic.response, 106
sanic.router, 109
sanic.server, 111
sanic.static, 116
sanic.testing, 117
sanic.views, 118
sanic.websocket, 119
135
Sanic Documentation, Release 19.12.2
137
Sanic Documentation, Release 19.12.2
138 Index
Sanic Documentation, Release 19.12.2
Index 139
Sanic Documentation, Release 19.12.2
140 Index
Sanic Documentation, Release 19.12.2
Index 141
Sanic Documentation, Release 19.12.2
142 Index
Sanic Documentation, Release 19.12.2
U
Unauthorized, 99
uri() (sanic.blueprints.FutureListener property), 95
uri() (sanic.blueprints.FutureRoute property), 96
uri() (sanic.blueprints.FutureStatic property), 96
uri() (sanic.router.Route property), 110
uri_template (sanic.request.Request attribute), 105
url (sanic.server.HttpProtocol attribute), 114
url (sanic.websocket.WebSocketProtocol attribute), 120
url() (sanic.request.Request property), 105
url_for() (sanic.app.Sanic method), 90
url_for() (sanic.request.Request method), 105
url_for() (sanic.Sanic method), 127
url_hash() (in module sanic.router), 111
url_prefix() (sanic.blueprint_group.BlueprintGroup
property), 97
URLBuildError, 99
V
version (sanic.request.Request attribute), 106
version() (sanic.blueprints.FutureRoute property), 96
W
wait_closed() (sanic.server.AsyncioServer method),
112
websocket() (sanic.app.Sanic method), 91
websocket() (sanic.Blueprint method), 131
websocket() (sanic.blueprints.Blueprint method), 94
websocket() (sanic.Sanic method), 127
websocket() (sanic.testing.SanicASGITestClient
method), 117
websocket() (sanic.testing.SanicTestClient method),
117
websocket_handshake()
(sanic.websocket.WebSocketProtocol method),
120
WebSocketConnection (class in sanic.websocket),
120
WebSocketProtocol (class in sanic.websocket), 119
write() (sanic.response.StreamingHTTPResponse
method), 107
Index 143