Routing

路由允许用户为不同的 URL 端指定处理函数。

一个基本的路由看起来如下,其中 app 是一个 Sanic 类的实例:

  1. from sanic.response import json
  2. @app.route("/")
  3. async def test(request):
  4. return json({ "hello": "world" })

http://server.url/ 这个 url 被访问 (服务器的根 url), 最后的 / 被路由匹配到 test 这个返回一个 JSON 对象的处理函数。

Sanic 处理函数必须使用 async def 语法来定义,因为他们是异步函数。

请求参数

Sanic 附带一个支持请求参数的基本路由。

要指定一个请求参数,用尖括号包裹如下: <PARAM>。请求参数作为关键字被传递到处理函数。

  1. from sanic.response import text
  2. @app.route('/tag/<tag>')
  3. async def tag_handler(request, tag):
  4. return text('Tag - {}'.format(tag))

要指定一个参数的类型,需在括号里定义的参数名后面加一个 :type 。如果参数匹配不到这个指定的类型, Sanic 将会抛出一个 NotFound 异常,在 URL 上以一个 404: Page not found 错误作为结果。

  1. from sanic.response import text
  2. @app.route('/number/<integer_arg:int>')
  3. async def integer_handler(request, integer_arg):
  4. return text('Integer - {}'.format(integer_arg))
  5. @app.route('/number/<number_arg:number>')
  6. async def number_handler(request, number_arg):
  7. return text('Number - {}'.format(number_arg))
  8. @app.route('/person/<name:[A-z]+>')
  9. async def person_handler(request, name):
  10. return text('Person - {}'.format(name))
  11. @app.route('/folder/<folder_id:[A-z0-9]{0,4}>')
  12. async def folder_handler(request, folder_id):
  13. return text('Folder - {}'.format(folder_id))

HTTP 请求方法

默认情况下,在一个 URL 上定义一个路由将只会对 GET 请求有效。然而, @app.route 装饰器接受一个可选的参数, methods,该参数允许处理函数处理 HTTP 请求方法列表中的任意一个方法。

  1. from sanic.response import text
  2. @app.route('/post', methods=['POST'])
  3. async def post_handler(request):
  4. return text('POST request - {}'.format(request.json))
  5. @app.route('/get', methods=['GET'])
  6. async def get_handler(request):
  7. return text('GET request - {}'.format(request.args))

还有一个可选的 host 参数 (可以是一个 list 或 string)。这限定了一个路由到提供的主机或主机组。如果一个路由没有 host,这会设置默认。

  1. @app.route('/get', methods=['GET'], host='example.com')
  2. async def get_handler(request):
  3. return text('GET request - {}'.format(request.args))
  4. # if the host header doesn't match example.com, this route will be used
  5. @app.route('/get', methods=['GET'])
  6. async def get_handler(request):
  7. return text('GET request in default - {}'.format(request.args))

还有简写方法装饰器:

  1. from sanic.response import text
  2. @app.post('/post')
  3. async def post_handler(request):
  4. return text('POST request - {}'.format(request.json))
  5. @app.get('/get')
  6. async def get_handler(request):
  7. return text('GET request - {}'.format(request.args))

add_route 方法

正如我们看到的,路由经常由 @app.route 装饰器来指定。然而,这个装饰器其实只是一个 app.add_route 方法的包装,用法如下:

  1. from sanic.response import text
  2. # Define the handler functions
  3. async def handler1(request):
  4. return text('OK')
  5. async def handler2(request, name):
  6. return text('Folder - {}'.format(name))
  7. async def person_handler2(request, name):
  8. return text('Person - {}'.format(name))
  9. # Add each handler function as a route
  10. app.add_route(handler1, '/test')
  11. app.add_route(handler2, '/folder/<name>')
  12. app.add_route(person_handler2, '/person/<name:[A-z]>', methods=['GET'])

URL 由 url_for 创建

Sanic 提供了一个 url_for 方法来生成一组基于这个处理方法名称的 URL。如果你想在 app 中避免硬编码 url,这是有用的。相反,你可以只参考这个处理名称。举个例子:

  1. from sanic.response import redirect
  2. @app.route('/')
  3. async def index(request):
  4. # generate a URL for the endpoint `post_handler`
  5. url = app.url_for('post_handler', post_id=5)
  6. # the URL is `/posts/5`, redirect to it
  7. return redirect(url)
  8. @app.route('/posts/<post_id>')
  9. async def post_handler(request, post_id):
  10. return text('Post - {}'.format(post_id))

在使用 url_for 时需要注意的其他事项:

  • 关键字参数传递给 url_for 不是请求参数将会被包括在 URL 的请求参数里。例如:
    1. url = app.url_for('post_handler', post_id=5, arg_one='one', arg_two='two')
    2. # /posts/5?arg_one=one&arg_two=two
  • 多元的参数也会被 url_for 传递。例如:
    1. url = app.url_for('post_handler', post_id=5, arg_one=['one', 'two'])
    2. # /posts/5?arg_one=one&arg_one=two
  • 还有一些特殊的参数 (_anchor, _external, _scheme, _method, _server) 传递到 url_for 会有一个指定的 url 建立 (_method 现在不被支持并且会被忽略)。例如: ```python url = app.url_for(‘post_handler’, post_id=5, arg_one=’one’, _anchor=’anchor’)

    /posts/5?arg_one=one#anchor

url = app.url_for(‘post_handler’, post_id=5, arg_one=’one’, _external=True)

//server/posts/5?arg_one=one

_external requires passed argument _server or SERVER_NAME in app.config or url will be same as no _external

url = app.url_for(‘post_handler’, post_id=5, arg_one=’one’, _scheme=’http’, _external=True)

http://server/posts/5?arg_one=one

when specifying _scheme, _external must be True

you can pass all special arguments one time

url = app.url_for(‘post_handler’, post_id=5, arg_one=[‘one’, ‘two’], arg_two=2, _anchor=’anchor’, _scheme=’http’, _external=True, _server=’another_server:8888’)

http://another_server:8888/posts/5?arg_one=one&arg_one=two&arg_two=2#anchor

  1. - 所有有效的参数必须被传递到 `url_for` 用来建立一个 URL。如果一个参数没有提供,或者没有匹配到指定的类型,会抛出一个 `URLBuildError`
  2. ## WebSocket 路由
  3. 对于 WebSocket 协议的路由可以使用 `@app.websocket`
  4. 装饰器定义:
  5. ```python
  6. @app.websocket('/feed')
  7. async def feed(request, ws):
  8. while True:
  9. data = 'hello!'
  10. print('Sending: ' + data)
  11. await ws.send(data)
  12. data = await ws.recv()
  13. print('Received: ' + data)

另外, app.add_websocket_route 方法可以用来替代这个装饰器:

  1. async def feed(request, ws):
  2. pass
  3. app.add_websocket_route(my_websocket_handler, '/feed')

一个 WebSocket 路由的处理程序作为第一个参数传递请求,同时一个 WebSocket 协议对象作为第二个参数。这个协议对象有 sendrecv 方法分别发送和接受数据。

支持 WebSocket 需要 Aymeric Augustin 提供的 websockets 包。

关于 strict_slashes

你可以设计严格的 routes 来决定要不要斜线。它是可配置的。

  1. # provide default strict_slashes value for all routes
  2. app = Sanic('test_route_strict_slash', strict_slashes=True)
  3. # you can also overwrite strict_slashes value for specific route
  4. @app.get('/get', strict_slashes=False)
  5. def handler(request):
  6. return text('OK')
  7. # It also works for blueprints
  8. bp = Blueprint('test_bp_strict_slash', strict_slashes=True)
  9. @bp.get('/bp/get', strict_slashes=False)
  10. def handler(request):
  11. return text('OK')
  12. app.blueprint(bp)

用户定义路由命名

你可以传递 name 来改变路由命名以避免使用默认命名 (handler.__name__)。

  1. app = Sanic('test_named_route')
  2. @app.get('/get', name='get_handler')
  3. def handler(request):
  4. return text('OK')
  5. # then you need use `app.url_for('get_handler')`
  6. # instead of # `app.url_for('handler')`
  7. # It also works for blueprints
  8. bp = Blueprint('test_named_bp')
  9. @bp.get('/bp/get', name='get_handler')
  10. def handler(request):
  11. return text('OK')
  12. app.blueprint(bp)
  13. # then you need use `app.url_for('test_named_bp.get_handler')`
  14. # instead of `app.url_for('test_named_bp.handler')`
  15. # different names can be used for same url with different methods
  16. @app.get('/test', name='route_test')
  17. def handler(request):
  18. return text('OK')
  19. @app.post('/test', name='route_post')
  20. def handler2(request):
  21. return text('OK POST')
  22. @app.put('/test', name='route_put')
  23. def handler3(request):
  24. return text('OK PUT')
  25. # below url are the same, you can use any of them
  26. # '/test'
  27. app.url_for('route_test')
  28. # app.url_for('route_post')
  29. # app.url_for('route_put')
  30. # for same handler name with different methods
  31. # you need specify the name (it's url_for issue)
  32. @app.get('/get')
  33. def handler(request):
  34. return text('OK')
  35. @app.post('/post', name='post_handler')
  36. def handler(request):
  37. return text('OK')
  38. # then
  39. # app.url_for('handler') == '/get'
  40. # app.url_for('post_handler') == '/post'

为静态资源建立 URL

现在你可以使用 url_for 给静态资源建立 url。如果是为文件直传的, filename 会被忽略。

  1. app = Sanic('test_static')
  2. app.static('/static', './static')
  3. app.static('/uploads', './uploads', name='uploads')
  4. app.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')
  5. bp = Blueprint('bp', url_prefix='bp')
  6. bp.static('/static', './static')
  7. bp.static('/uploads', './uploads', name='uploads')
  8. bp.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')
  9. app.blueprint(bp)
  10. # then build the url
  11. app.url_for('static', filename='file.txt') == '/static/file.txt'
  12. app.url_for('static', name='static', filename='file.txt') == '/static/file.txt'
  13. app.url_for('static', name='uploads', filename='file.txt') == '/uploads/file.txt'
  14. app.url_for('static', name='best_png') == '/the_best.png'
  15. # blueprint url building
  16. app.url_for('static', name='bp.static', filename='file.txt') == '/bp/static/file.txt'
  17. app.url_for('static', name='bp.uploads', filename='file.txt') == '/bp/uploads/file.txt'
  18. app.url_for('static', name='bp.best_png') == '/bp/static/the_best.png'