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)
demo:
from sanic.response import json
@app.route(“/“)
async def test(request):
return json({ “ hello ”: “ world ” })
demo
from sanic.response import text
@app.route(‘/tag/<tag>‘)
async def tag_handler(request, tag):
return text(‘ Tag - {}’.format(tag))
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))
demo_1
from sanic.response import text
@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))
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'])
demo
@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))
url = app.url_for('post_handler', post_id=5, arg_one='one', arg_two='two')
#/posts/5?arg_one=one&arg_two=two
url = app.url_for('post_handler', post_id=5, arg_one=['one', 'two'])
# /posts/5?arg_one=one&arg_one=two
demo:
@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')
from sanic import response
@app.route('/text')
def handle_request(request):
return response.text('Hello world!')
from sanic import response
@app.route('/html')
def handle_request(request):
return response.html('<p>Hello world!</p>')
from sanic import response
@app.route('/json')
def handle_request(request):
return response.json({'message': 'Hello world!'})
from sanic import response
@app.route('/file')
async def handle_request(request):
return await response.file('/srv/www/whatever.png')
from sanic import response
@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')
from sanic import response
@app.route('/big_file.png')
async def handle_request(request):
return await response.file_stream('/srv/www/whatever.png')
from sanic import response
@app.route('/redirect')
def handle_request(request):
return response.redirect('/json')
from sanic import response
@app.route(‘/raw ’)
def handle_request(request):
return response.raw(‘ raw data ’)
from sanic import response
@app.route(‘/json ’)
def handle_request(request):
return response.json(
{‘ message ’: ‘ Hello world!’},
headers={‘ X-Served-By ’: ‘ sanic ’},
status=200
)
更多的内容:Sanic 中文文档
1
markjack OP 这是作者自己翻译的,在我的产品上发布,好像百度目前还搜不到 sanic 相关的中文文档,觉得非常不错,我就顺便帮忙推广下,大家多多支持,也多多支持我的产品
|
2
PythonAnswer 2017-10-31 13:25:55 +08:00 via Android
sanic 作者自己会中文吗,还不知道他是中国人
|
3
markjack OP @PythonAnswer 哈哈哈 抱歉 我表达的问题,是有位用户翻译了关于 sanic 文档,因为目前 sanic 中文的教程比较少,觉得这个问答很有用,就帮他推广下
|
4
takanasi 2017-10-31 13:44:21 +08:00
|
5
XIVN1987 2017-10-31 13:46:10 +08:00
感觉“ async def ”这种语法很丑啊,,为什么不直接“ async ”呢??就算用以前的 @asyncio.coroutine 修饰符也比“ async def ”好看
|
7
takanasi 2017-10-31 13:57:01 +08:00
|
10
markjack OP @XIVN1987 因人而异吧,python3 用 async def 来定义协程函数,可能是觉得这种写法更像写函数,我觉得这样挺好,一眼就知道这个函数是一个协程函数
|
12
est 2017-10-31 14:07:06 +08:00
拉到最后,终于看到链接了。
|
14
kongkongyzt 2017-10-31 14:26:31 +08:00 via Android
想问下 sanic 有哪些异步 mysql driver 搭配一起使用
|
15
XIVN1987 2017-10-31 14:30:53 +08:00
@takanasi
确实是我孤陋寡闻了,,刚刚查了下 async def、async for、async with 是在 Python3.5 中一起新添加的特性 |
16
markjack OP |
17
hcnhcn012 2017-10-31 15:14:40 +08:00 via iPhone
|
18
n329291362 2017-10-31 16:42:47 +08:00
@kongkongyzt aiomysql
|
19
4everLoveU 2017-10-31 17:15:00 +08:00
这语法,我咋感觉是在 flask 框架基础上自己封装出来的?
也没有说高性能表现在哪里,和 flask、tornado 比有什么优势,解决了啥问题。 可以讲讲这些 |
20
julyclyde 2017-11-01 13:52:04 +08:00
看这文档,觉得也就能类比 pocoo 那边的 Werkzeug toolkit
|