Python高性能Web框架Sanic中文文档如何使用
入门指南
- Install Sanic:python3 -m pip install sanic
example
from sanic import Sanic
from sanic.response import text
app = Sanic(name)
@app.route(“/“)
async def test(request):
return text(‘ Hello world!’)
app.run(host=” 0.0.0.0 ”, port=8000, debug=True)
路由
- 路由允许用户为不同的 URL 端点指定处理程序函数。
demo:
from sanic.response import json
[@app](/user/app).route(“/“)
async def test(request):
return json({ “ hello ”: “ world ” })
- url http://server.url/被访问(服务器的基本 url),最终'/'被路由器匹配到处理程序函数,测试,然后返回一个 JSON 对象。
Request parameters (请求参数)
- 要指定一个参数,可以用像这样的角引号<param>包围它。请求参数将作为关键字参数传递给路线处理程序函数。
demo
from sanic.response import text
[@app](/user/app).route(‘/tag/<tag>‘)
async def tag_handler(request, tag):
return text(‘ Tag - {}’.format(tag))
- 为参数指定类型,在参数名后面添加(:类型)。如果参数不匹配指定的类型,Sanic 将抛出一个不存在的异常,导致一个 404 页面
demo:
from sanic.response import text
@app.route(‘/number/<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(‘/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))
HTTP request types(请求类型)
- 路由装饰器接受一个可选的参数,方法,它允许处理程序函数与列表中的任何 HTTP 方法一起工作。
demo_1
from sanic.response import text
[@app](/user/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))
demo_2
from sanic.response import text
@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))
add_route method(增加路由)
from sanic.response import text
# Define the handler functions
async def handler1(request):
return text('OK')
async def handler2(request, name):
return text('Folder - {}'.format(name))
async def person_handler2(request, name):
return text('Person - {}'.format(name))
# Add each handler function as a route
app.add_route(handler1, '/test')
app.add_route(handler2, '/folder/<name>')
app.add_route(person_handler2, '/person/<name:[A-z]>', methods=['GET'])
url_for
- Sanic 提供了一个 urlfor 方法,根据处理程序方法名生成 url。避免硬编码 url 路径到您的应用程序
demo
[@app](/user/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](/user/app).route('/posts/<post_id>')
async def post_handler(request, post_id):
return text('Post - {}'.format(post_id))
注意:
- 给 url equest 的关键字参数不是请求参数,它将包含在 URL 的查询字符串中。例如:
url = app.url_for('post_handler', post_id=5, arg_one='one', arg_two='two')
#/posts/5?arg_one=one&arg_two=two
- 所有有效的参数必须传递给 url 以便构建一个 URL。如果没有提供一个参数,或者一个参数与指定的类型不匹配,就会抛出一个 URLBuildError 可以将多值参数传递给 url
url = app.url_for('post_handler', post_id=5, arg_one=['one', 'two'])
# /posts/5?arg_one=one&arg_one=two
WebSocket routes(网络套接字路由)
- websocket 可以通过装饰路由实现
demo:
[@app](/user/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)
另外,添加 websocket 路由方法可以代替装饰器
async def feed(request, ws):
pass
app.add_websocket_route(my_websocket_handler, '/feed')
响应( response )
text
from sanic import response
[@app](/user/app).route('/text')
def handle_request(request):
return response.text('Hello world!')
HTML
from sanic import response
[@app](/user/app).route('/html')
def handle_request(request):
return response.html('<p>Hello world!</p>')
JSON
from sanic import response
[@app](/user/app).route('/json')
def handle_request(request):
return response.json({'message': 'Hello world!'})
File
from sanic import response
[@app](/user/app).route('/file')
async def handle_request(request):
return await response.file('/srv/www/whatever.png')
Streaming
from sanic import response
[@app](/user/app).route("/streaming")
async def index(request):
async def streaming_fn(response):
response.write('foo')
response.write('bar')
return response.stream(streaming_fn, content_type='text/plain')
File Streaming
- 对于大文件,文件和流的组合
from sanic import response
[@app](/user/app).route('/big_file.png')
async def handle_request(request):
return await response.file_stream('/srv/www/whatever.png')
Redirect
from sanic import response
[@app](/user/app).route('/redirect')
def handle_request(request):
return response.redirect('/json')
Raw
- 没有进行编码的响应
from sanic import response
[@app](/user/app).route(‘/raw ’)
def handle_request(request):
return response.raw(‘ raw data ’)
Modify headers or status
- 要修改头或状态代码,将标题或状态参数传递给这些函数
from sanic import response
[@app](/user/app).route(‘/json ’)
def handle_request(request):
return response.json(
{‘ message ’: ‘ Hello world!’},
headers={‘ X-Served-By ’: ‘ sanic ’},
status=200
)
更多的内容:Sanic 中文文档
Python高性能Web框架Sanic中文文档如何使用
这是作者自己翻译的,在我的产品上发布,好像百度目前还搜不到 sanic 相关的中文文档,觉得非常不错,我就顺便帮忙推广下,大家多多支持,也多多支持我的产品
Sanic的中文文档使用起来挺直接的,主要就几个地方。
首先,官方主站(sanic.dev)的文档是英文的,但国内社区维护了一个中文文档站点。你直接搜“Sanic 中文文档”或者访问 sanic-org.cn 应该就能找到。这个站点内容和官方基本同步,界面也是中文的。
进去之后,左侧是导航栏,结构很清晰:从“入门指南”、“指南”到“高级”主题都有。比如你想快速跑起来,就看“入门指南”里的“快速开始”;想深入了解路由、请求响应、中间件这些核心概念,就去“指南”部分。每个页面都结合代码示例讲解,直接复制就能用。
这里有个简单的示例,演示了最基本的路由和异步响应,和文档里“快速开始”部分类似:
from sanic import Sanic
from sanic.response import text
app = Sanic("MyApp")
@app.get("/")
async def hello(request):
return text("Hello, world!")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)
文档里对于像路由参数(/user/<id:int>)、不同的响应类型(JSON、HTML、文件流)、蓝图这些常用功能都有专门章节,例子给得挺全。遇到问题可以多用页面顶部的搜索框,或者直接去GitHub仓库的Issue里看看有没有类似讨论。
总结:直接访问中文社区站,按导航查阅,配合示例代码上手最快。
哈哈哈 抱歉 我表达的问题,是有位用户翻译了关于 sanic 文档,因为目前 sanic 中文的教程比较少,觉得这个问答很有用,就帮他推广下
感觉“ async def ”这种语法很丑啊,,为什么不直接“ async ”呢??就算用以前的 .coroutine 修饰符也比“ async def ”好看
什么?
因为还有async for语句啊
原来如此,,多谢。。
因人而异吧,python3 用 async def 来定义协程函数,可能是觉得这种写法更像写函数,我觉得这样挺好,一眼就知道这个函数是一个协程函数
这是作者编辑问题,正在跟他联系
拉到最后,终于看到链接了。
这个你都不知道?我告诉你其实还有 async with
想问下 sanic 有哪些异步 mysql driver 搭配一起使用
确实是我孤陋寡闻了,,刚刚查了下 async def、async for、async with 是在 Python3.5 中一起新添加的特性
我也挺习惯用 .coroutine 的,3.4 的语法改不过来了。。。
aiomysql
这语法,我咋感觉是在 flask 框架基础上自己封装出来的?
也没有说高性能表现在哪里,和 flask、tornado 比有什么优势,解决了啥问题。
可以讲讲这些
看这文档,觉得也就能类比 pocoo 那边的 Werkzeug toolkit


