您好,欢迎访问一九零五行业门户网

Python中Tornado的路由解析(附实例)

本篇文章给大家带来的内容是关于python中tornado的路由解析(附实例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
向web.application对象传递的第1个参数url路由映射列表的配置方式与django类型,用正则字符串进行路由匹配。
tornado的路由字符串有两种,固定字符串路径和参数字符串路径
1、固定字串路径
固定字符串即是普通的字符串固定匹配,比如:
handlers=[(/,mainhandler), #只匹配跟路径(/entry,entryhandler) #只匹配/entry(/entry/2019,entry2019handler) #只匹配/entry/2019]
2、参数字符路径:正在表达式定义路径
参数子串可以将具备一定模式的路径映射到同一个requesthandler中处理,其中路径中的参数部分用小括号()标识。
实例:参数路径
import tornado.ioloopimport tornado.webclass mainhandler(tornado.web.requesthandler):    def get(self,id):        self.write(hello world+id)def make_app():    return tornado.web.application([        (/id/([^/]+),mainhandler),    ])def main():    app=make_app()    app.listen(8888)    tornado.ioloop.ioloop.current().start()if __name__==__main__:    main()
在浏览器输入:http://localhost:8888/id/666
页面输出:
hello world666
其中的/id/([^/]+)是正在表达式。可以匹配:
http://xxx.xxx.xxx/id/xxx
但是无法匹配:
http://xxx.xxx.xxx/id
要想也匹配这个字符,可以修改正在表达式,将:/id/([^/]+)改为/id/([^/]+)即可。
以上就是python中tornado的路由解析(附实例)的详细内容。
其它类似信息

推荐信息