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 中文文档
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.