Speed comparison
Framework implementation base requests per second average time
Sanic Python 3.5 + uvloop 30,601 3.23ms
Wheezy Gunicorn + Meinheld 20,244 4.94ms
Falcon Gunicorn + Meinheld 18,972 5.27ms
Bottle Gunicorn + Meinheld 13,596 7.36ms
Flask Gunicorn + Meinheld 4,988 20.08ms
Kyoukai Python 3.5 + uvloop 3,889 27.44ms
Aiohttp Python 3.5 + uvloop 2,979 33.42ms
Installation
Environment: python3.5+?
python-m pip Install Sanic
Hello World
Create a file main.py, write the following content
1.from sanic Import Sanic
2.from sanic.response Import JSON
3.
4.app = Sanic (
name) br/>5.
[email protected]("/")
- ?? return json ({"Hello": "World"})
10.app.run (host= "0.0.0.0", port=8000)
Run? Python3 main.py?
Does sanic look like flask?
Request
Property?
Request.Files (Dictionary of File objects)-List of uploaded files?
Request.json (any)-JSON data?
Request.args (dict)-get data?
Request.Form (dict)-Post form data
Example
1.from sanic Import Sanic
2.from sanic.response Import JSON
[email protected] ("/json")
5.def Post_json (Request):
- ?? return JSON ({"Received": True, "message": Request.json})
[email protected] ("/form")
9.def Post_json (Request):
- ?. return json ({"Received": True, "Form_data": Request.Form, "Test": request.form.get (' Test ')})
[email protected] ("/files")
13.def Post_json (Request):
- ?? test_file = Request.files.get (' Test ')
- ?? file_parameters = {
- ? ? ? ?‘ Body ': Test_file.body,
- ? ? ? ?‘ Name ': Test_file.name,
- ? ? ? ?‘ Type ': Test_file.type,
- ? ?}
- ?. return json ({"Received": True, "File_names": Request.files.keys (), "Test_file_parameters": File_parameters})
[email protected] ("/query_string")
25.def query_string (Request):
- ?. return json ({"Parsed": True, "args": Request.args, "url": Request.url, "query_string": request.query_string})
Routing
And flask Almost, a look at it
1.from sanic Import Sanic
2.from sanic.response Import Text
[email protected] ('/tag/')
5.async def person_handler (request, TAG):
- ?? return text (' tag-{} '. Format (TAG))
[email protected] ('/number/')
9.async def person_handler (Request, Integer_arg):
- ?? return text (' Integer-{} '. Format (INTEGER_ARG))
[email protected] ('/number/')
13.async def person_handler (Request, Number_arg):
- ?? return text (' number-{} '. Format (number))
[email protected] ('/person/')
17.async def person_handler (Request, name):
- ?? return text (' Person-{} '. Format (name))
[email protected] ('/folder/')
21.async def folder_handler (Request, folder_id):
- ?? return text (' Folder-{} '. Format (folder_id))
Register middleware
1.app = sanic (name)
[Email protected]
4.async def halt_request (Request):
- ?? print ("I am a Spy")
[email protected] (' request ')
8.async def halt_request (Request):
- ?? return text (' I halted the request ')
[email protected] (' response ')
12.async def halt_response (Request, Response):
- ?? return text (' I halted the response ')
[email protected] (‘/‘)
16.async def handler (request):
- ?? return text (' I would like to speak now ')
19.app.run (host= "0.0.0.0", port=8000)
Exception handling
Throw exception
1.from sanic Import Sanic
2.from sanic.exceptions Import Servererror
[email protected] ('/killme ')
5.def I_am_ready_to_die (Request):
- ?? Raise Servererror ("Something bad Happened")
Handling Exceptions
1.from sanic Import Sanic
2.from sanic.response Import Text
3.from sanic.exceptions Import notfoundbr/>[email protected](NotFound)
< li="">
- ?? return text ("Yep, I totally found the page: {}". Format (Request.url))
Blueprint
Like the blueprints in flask, to organize the project structure?
Create a blueprint that is equivalent to creating a Sanic app with the same usage as above and changing the app to a blueprint name BP
1.from sanic.response Import JSON
2.from sanic Import Blueprint
4.BP = Blueprint (' My_blueprint ')
[email protected] (‘/‘)
7.async def bp_root ():
- ?? return json ({' My ': ' Blueprint '})
Blueprint registration to the main app
1.from sanic Import Sanic
2.from My_blueprint Import bp
4.app = sanic (name)
5.app.register_blueprint (BP)
7.app.run (host= ' 0.0.0.0 ', port=8000, Debug=true)
Summarize
Sanic will be a very popular framework. Because it's based on python3.5+, it uses many new features that make the program faster.
* Disclaimer: Push content and images from the Internet, some of the content will be changed, the copyright is owned by the original author, such as the source of incorrect information or infringement of interests, please contact us for removal or authorization matters.
Getting started with the fastest Python WEB framework