路由

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

基本路线如下所示,其中 app 是的实例 Sanic 班级:

from sanic.response import json

@app.route("/")
async def test(request):
    return json({ "hello": "world" })

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

必须使用 async def 语法,因为它们是异步函数。

请求参数

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

若要指定参数,请使用角引号将其括起来,如下所示: <PARAM> . 请求参数将作为关键字参数传递给路由处理程序函数。

from sanic.response import text

@app.route('/tag/<tag>')
async def tag_handler(request, tag):
    return text('Tag - {}'.format(tag))

若要指定参数的类型,请添加 :type 在参数名后面,引号内。如果参数与指定的类型不匹配,Sanic将抛出 NotFound 异常,导致 404: Page not found URL错误。

支持的类型

  • string
    • “鲍勃”

    • “ Python 3”

  • int
    • 10

    • 20

    • 30

    • -10

    • (这里没有浮点数)

  • number
    • 1

    • 1.5

    • 10

    • -10

  • alpha
    • “鲍勃”

    • “ Python ”

    • (如果它包含符号或非字母数字字符,它将失败)

  • path
    • “你好”

    • "hello.text"

    • “你好,世界”

  • uuid
    • 123A123A-A12A-1A1A-A1A1-1A12A1A12345(Uuidv4支持)

  • regex expression

如果未设置类型,则应为字符串。为函数提供的参数将始终是一个字符串,与类型无关。

from sanic.response import text

@app.route('/string/<string_arg:string>')
async def string_handler(request, string_arg):
    return text('String - {}'.format(string_arg))

@app.route('/int/<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('/alpha/<alpha_arg:alpha>')
async def number_handler(request, alpha_arg):
    return text('Alpha - {}'.format(alpha_arg))

@app.route('/path/<path_arg:path>')
async def number_handler(request, path_arg):
    return text('Path - {}'.format(path_arg))

@app.route('/uuid/<uuid_arg:uuid>')
async def number_handler(request, uuid_arg):
    return text('Uuid - {}'.format(uuid_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))

警告

str is not a valid type tag. If you want str recognition then you must use string

HTTP请求类型

默认情况下,在URL上定义的路由将仅对该URL的GET请求可用。然而 @app.route decorator接受一个可选参数, methods ,它允许handler函数处理列表中的任何HTTP方法。

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

还有一个可选的 host 参数(可以是列表或字符串)。这会将路由限制到所提供的一台或多台主机。如果也有一条没有主机的路由,则它将是默认路由。

@app.route('/get', methods=['GET'], host='example.com')
async def get_handler(request):
    return text('GET request - {}'.format(request.args))

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

还有一些速记方法装饰师:

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

这个 add_route 方法

正如我们所看到的,通常使用 @app.route 装饰工。然而,这个装饰器实际上只是 app.add_route 方法,使用如下:

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

URL生成方式 url_for

Sanic提供 url_for 方法,以根据处理程序方法名生成URL。如果您希望避免将url路径硬编码到应用程序中,这将非常有用;相反,您可以只引用处理程序名称。例如:

from sanic.response import redirect

@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_for

  • 传递给的关键字参数 url_for 非请求参数将包含在URL的查询字符串中。例如:

url = app.url_for('post_handler', post_id=5, arg_one='one', arg_two='two')
# /posts/5?arg_one=one&arg_two=two
  • 多值参数可以传递给 url_for . 例如:

url = app.url_for('post_handler', post_id=5, arg_one=['one', 'two'])
# /posts/5?arg_one=one&arg_one=two
  • 还有一些特殊的论点 (_anchor, _ 外部_scheme, _ 方法_server) passed to url_for will have special url building (`_ 方法“”现在不受支持,将被忽略)。例如:

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 you to pass an argument _server or set SERVER_NAME in app.config if not 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 at once
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
  • 必须将所有有效参数传递给 url_for 创建URL。如果未提供参数,或者参数与指定类型不匹配,则 URLBuildError 将被提升。

WebSocket路由

WebSocket协议的路由可以用 @app.websocket 装饰者:

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

或者, app.add_websocket_route 方法可以代替修饰器:

async def feed(request, ws):
    pass

app.add_websocket_route(my_websocket_handler, '/feed')

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

WebSocket支持需要 websockets Aymeric Augustin包装。

关于 strict_slashes

你可以做 routes 是否严格到尾随斜杠,它是可配置的。

# provide default strict_slashes value for all routes
app = Sanic('test_route_strict_slash', strict_slashes=True)

# you can also overwrite strict_slashes value for specific route
@app.get('/get', strict_slashes=False)
def handler(request):
    return text('OK')

# It also works for blueprints
bp = Blueprint('test_bp_strict_slash', strict_slashes=True)

@bp.get('/bp/get', strict_slashes=False)
def handler(request):
    return text('OK')

app.blueprint(bp)

如何 strict_slashes 标志遵循定义的层次结构,该层次结构决定特定路由是否属于 strict_slashes 行为。

路线/
├——蓝图/
├——应用/

上面的层次结构定义了 strict_slashes 旗子会起作用的。第一个非 None 价值观 strict_slashes 在上述顺序中找到的将应用于所讨论的路线。

from sanic import Sanic, Blueprint
from sanic.response import text

app = Sanic("sample_strict_slashes", strict_slashes=True)

@app.get("/r1")
def r1(request):
    return text("strict_slashes is applicable from App level")

@app.get("/r2", strict_slashes=False)
def r2(request):
    return text("strict_slashes is not applicable due to  False value set in route level")

bp = Blueprint("bp", strict_slashes=False)

@bp.get("/r3", strict_slashes=True)
def r3(request):
    return text("strict_slashes applicable from blueprint route level")

bp1 = Blueprint("bp1", strict_slashes=True)

@bp.get("/r4")
def r3(request):
    return text("strict_slashes applicable from blueprint level")

用户定义的路由名称

自定义路由名可以通过传递 name 在注册将重写使用 handler.__name__ 属性。

app = Sanic('test_named_route')

@app.get('/get', name='get_handler')
def handler(request):
    return text('OK')

# then you need use `app.url_for('get_handler')`
# instead of # `app.url_for('handler')`

# It also works for blueprints
bp = Blueprint('test_named_bp')

@bp.get('/bp/get', name='get_handler')
def handler(request):
    return text('OK')

app.blueprint(bp)

# then you need use `app.url_for('test_named_bp.get_handler')`
# instead of `app.url_for('test_named_bp.handler')`

# different names can be used for same url with different methods

@app.get('/test', name='route_test')
def handler(request):
    return text('OK')

@app.post('/test', name='route_post')
def handler2(request):
    return text('OK POST')

@app.put('/test', name='route_put')
def handler3(request):
    return text('OK PUT')

# below url are the same, you can use any of them
# '/test'
app.url_for('route_test')
# app.url_for('route_post')
# app.url_for('route_put')

# for same handler name with different methods
# you need specify the name (it's url_for issue)
@app.get('/get')
def handler(request):
    return text('OK')

@app.post('/post', name='post_handler')
def handler(request):
    return text('OK')

# then
# app.url_for('handler') == '/get'
# app.url_for('post_handler') == '/post'

为静态文件生成URL

Sanic支持使用 url_for 方法来生成静态文件URL。如果静态url指向一个目录, filename 参数 url_for 可以忽略。

app = Sanic('test_static')
app.static('/static', './static')
app.static('/uploads', './uploads', name='uploads')
app.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')

bp = Blueprint('bp', url_prefix='bp')
bp.static('/static', './static')
bp.static('/uploads', './uploads', name='uploads')
bp.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')
app.blueprint(bp)

# then build the url
app.url_for('static', filename='file.txt') == '/static/file.txt'
app.url_for('static', name='static', filename='file.txt') == '/static/file.txt'
app.url_for('static', name='uploads', filename='file.txt') == '/uploads/file.txt'
app.url_for('static', name='best_png') == '/the_best.png'

# blueprint url building
app.url_for('static', name='bp.static', filename='file.txt') == '/bp/static/file.txt'
app.url_for('static', name='bp.uploads', filename='file.txt') == '/bp/uploads/file.txt'
app.url_for('static', name='bp.best_png') == '/bp/static/the_best.png'