Home  >  Article  >  Backend Development  >  Route analysis of Tornado in Python (with examples)

Route analysis of Tornado in Python (with examples)

不言
不言forward
2018-10-16 16:16:332906browse

The content of this article is about the routing analysis of Tornado in Python (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The first parameter URL route mapping list passed to the web.Application object is configured in the same way as the Django type, using regular strings for route matching.

Tornado has two routing strings, fixed string path and parameter string path

1. Fixed string path

Fixed string is an ordinary string fixed match, for example:

Handlers=[
("/",MainHandler), #只匹配跟路径
("/entry",EntryHandler) #只匹配/entry
("/entry/2019",Entry2019Handler) #只匹配/entry/2019
]

2. Parameter character path: Expression definition path

Parameter substring can map a path with a certain pattern to the same RequestHandler for processing, where the parameter part in the path is identified by parentheses "()".

Example: Parameter path

import tornado.ioloop
import tornado.web

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

Input in the browser: http://localhost:8888/id/666

Page output:

Hello World666

where The /id/([^/] ) is the expression. Can match:

http://xxx.xxx.xxx/id/xxx

but cannot match:

http://xxx.xxx.xxx/id

If you want to match this character, you can modify the current expression and change: /id/([^/] ) to /id/ ([^/] ) will do.

The above is the detailed content of Route analysis of Tornado in Python (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete