Flask 路由映射对于双斜线的处理 //a//b

例子

from flask import Flask
import time
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop



app = Flask(__name__)

@app.route('//abc//a')
def index():
    # time.sleep(5)
    return 'OK'

@app.route('/abc//a')
def index1():
    return 'hhhh'

if __name__ == '__main__':
    http_server = HTTPServer(WSGIContainer(app))
    http_server.listen(5000)
    IOLoop.instance().start()

对于Flask,如上面这样的路由映射,是如何处理的呢?

  我们如果运行一下会发现,我们无论访问 'http://127.0.0.1:5000//abc//a' 还是 'http://127.0.0.1:5000/abc//a'返回的结果都是函数index1的结果,这是为什么?

分析源代码,debug我们发现,在werkzeug/routing.py中的1530行,path传递的过程中,werkzeug会将path_info-->>' //abc//a '的左侧的 ' / '去掉,然后再和前面的

http://127.0.0.1:5000拼接,所以访问'http://127.0.0.1:5000//abc//a'这个路由会路由到 'http://127.0.0.1:5000/abc//a',返回的是index1函数的结果。

 

posted @ 2019-07-17 20:47  留雁  阅读(527)  评论(0编辑  收藏  举报