Sanic route() 方法/函数

Sanic教程 2019-06-04 17:31:25 阅读(4282) 评论(0)

Sanic 类的route()方法的API接口。

Sanic route() 方法函数

 

route() 方法/函数

定义

route(uri, methods=frozenset({'GET'}), host=None, strict_slashes=None, stream=False, version=None, name=None)

装饰要被注册为路由的函数。

参数

  • uri :URL的路径
  • methods: 允许的请求方法的列表或元组。
  • host:服务允许的HOST地址
  • strict_slashes:严格匹配末尾的斜杠/ ,默认为False,即最后的/可有可无。
  • stream:指定路由函数是否是流处理函数。
  • version:API版本。
  • name:为url_for()方法定义的路由名称。

**返回值**
修饰过的函数

**例子**
```python
from sanic import Sanic
from sanic import response

app = Sanic(__name__)

••••
@app.route('/')
def handle_request(request):
    return response.redirect('/redirect')


@app.route('/redirect')
async def test(request):
    return response.json({"Redirected": True})


if __name__ == '__main__':
    app.run(host="0.0.0.0", port=8000)

猿人学banner宣传图

我的公众号:猿人学 Python 上会分享更多心得体会,敬请关注。

***版权申明:若没有特殊说明,文章皆是猿人学 yuanrenxue.con 原创,没有猿人学授权,请勿以任何形式转载。***

说点什么吧...