阿吉的Sanic教程--06路由配置

6. 路由

你的鼓励是我前进的动力,请为我点个赞吧!

路由的使用允许开发者自定义来自不同url的视图函数。最基本的路由如下所所示,其中app为Sainc的一个基本实例:

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

当url(http://server.url/)访问服务器时,会被相应的处理函数接受请求,并进行处理。Sanic的处理视图函数要使用async def定义,因为他们是异步函数。

(1)请求参数

sanic基于路由处理函数并可以让开发者自定义请求参数。开发者可以使用尖括号包参数,如所示;请求参数要被处理成关键字参数传递给视图函数。

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

开发者还可以为请求参数设置类型,如<param:type>所示,如果sanic没有找到类型说明,则sanic会出现 not found的报错。

  from sanic.response import text
    @app.route('/number/<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('/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))

(2)http 请求类型

默认情况下一个路由的处理函数只能为该路由提供get请求方式,不过,@app.route具有一个请求方式参数选项method,该方法为路由处理方式请求方式,此方法和flask一致需要开发者定义请求方式列表,具体代码如下所示:

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

sanic的route提供了一个host参数用于匹配请求的host,这个参数可以有效的防止爬虫干扰,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))

sanic的route还提供了快速的访问方式限制,具体如下所示:
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))

(3)add_route 方法

sainc 还提供了一种路由添加方法,app.add_route(url,function)这不是装饰器,而是一种简单的添加路由的方法,具体示例代码如下:

from sanic.response import text

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

app.add_route(handler1, '/test')
app.add_route(handler2, '/folder/<name>')
app.add_route(person_handler2, '/person/<name:[A-z]>', methods=['GET'])

(4)使用url_for 构建url

sanic提供了url_for构建url,使用url_for进行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 = 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_for还可以标示其他特殊参数(_anchor,燺external,燺scheme,燺method,燺server)
,具体如下所示:

  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

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

sanic提供了如下方式进行代替:

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

websocket处理函数必须有两个参数,第一个是request参数,第二个是ws协议对象,否则不能正常执行。

(6)关于“/”的使用

开发者可以规定是否严格匹配“/”

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

具体示例代码如下所示。

(7)用户定义路由名字

开发者可以为路由定义路由名字使用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

(8)为静态文件构建url

sanic支持开发者自定义框架静态文件路径,具体示例代码如下:

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'
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值