响应数据
使用sanic.response模块中的函数来创建响应。响应数据类型如下:
文本
from sanic import response
@app.route('/text')
def handle_request(request):
return response.text('Hello world!')
HTML
from sanic import response
@app.route('/html')
def handle_request(request):
return response.html('<p>Hello world!</p>')
JSON
from sanic import response
@app.route('/json')
def handle_request(request):
return response.json({'message': 'Hello world!'})
File文件类型
from sanic import response
@app.route('/file')
async def handle_request(request):
return await response.file('/srv/www/whatever.png')
Stream(流)
from sanic import response
@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')
重定向
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(b'raw data')
空数据
用于响应RFC 2616定义的空消息