路由

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

基本路径如下所示,其中app是sanic类的一个实例:

from sanic.response import json

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

当访问url http://server.url/时(服务器的基本url),路由器会将final/与处理程序函数test匹配,然后返回JSON对象。

SANIC处理程序函数必须使用异步定义语法定义,因为它们是异步函数。

请求参数

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

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

from sanic.response import text

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

要为参数指定类型,请在参数名称后面的引号内添加:type。如果参数与指定的类型不匹配,SANIC将引发NotFound异常,导致URL上出现404:Page Not Found错误。

支持的类型

  • 一串

    • “鲍勃”

    • “ Python 3”

  • int

    • 10

    • 20

    • 30

    • -10

    • (这里没有浮点数)

    • 1

    • 1.5

    • 10

    • -10

  • 阿尔法

    • “鲍勃”

    • “ Python ”

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

  • 路径

    • “你好”

    • "hello.text"

    • “你好,世界”

  • UUID

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

  • 正则表达式

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

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不是有效的类型标记。如果需要str识别,则必须使用字符串

HTTP请求类型

默认情况下,在URL上定义的路由只能用于获取对该URL的请求。但是,@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))

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

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

添加路由方法

如我们所见,通常使用@app.route decorator指定路由。但是,这个修饰器实际上只是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构建

sanic为方法提供了一个url_,以根据处理程序方法名生成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时要记住的其他事项:

  • 传递给url_的不是请求参数的关键字参数将包含在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 = app.url_for('post_handler', post_id=5, arg_one=['one', 'two'])
# /posts/5?arg_one=one&arg_one=two
  • 另外,传递给url的一些特殊参数(_anchor、_external、_scheme、_method、_server)将具有特殊的url构建(_method现在不受支持,将被忽略)。例如:

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_才能生成url。如果未提供参数,或者参数与指定的类型不匹配,则将引发urlbuild错误。

WebSocket路由

websocket协议的路由可以通过@app.websocket decorator定义:

@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方法代替decorator:

async def feed(request, ws):
    pass

app.add_websocket_route(my_websocket_handler, '/feed')

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

WebSocket支持需要Aymeri Augustin提供的WebSockets包。

关于严格的削减

您可以使路由严格到尾随斜线,或者不严格到尾随斜线,这是可配置的。


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

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

|___ Route
     |___ Blueprint
            |___ Application

上面的层次结构定义了“严格斜杠”标志的行为方式。上述顺序中找到的第一个严格斜线的非“无”值将应用于有问题的路由。

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

用户定义的路由名称

可以通过在注册路由时传递名称参数来使用自定义路由名称,该参数将覆盖使用 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_生成静态文件url的方法。如果静态URL指向某个目录,则可以忽略URL_的文件名参数。Q


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'