Skip to content
This repository has been archived by the owner on May 30, 2021. It is now read-only.

Latest commit

 

History

History
106 lines (75 loc) · 1.81 KB

response.md

File metadata and controls

106 lines (75 loc) · 1.81 KB

响应

使用 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!'})

文件

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')

Raw

未编码的响应体

from sanic import response

@app.route('/raw')
def handle_request(request):
    return response.raw(b'raw data')

修改响应头部和状态码

向那些函数中传入 headersstatus 参数可以修改响应头部或状态码:

from sanic import response

@app.route('/json')
def handle_request(request):
    return response.json(
        {'message': 'Hello world!'},
        headers={'X-Served-By': 'sanic'},
        status=200
    )