Route analysis of Tornado in Python (with examples)

不言
Release: 2018-10-16 17:47:04
forward
2918 people have browsed it

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
]
Copy after login

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()
Copy after login

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

Page output:

Hello World666
Copy after login

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

http://xxx.xxx.xxx/id/xxx
Copy after login

but cannot match:

http://xxx.xxx.xxx/id
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template