Python automatically registers Tornado routes with the decorator.
First version
In this version, the RouterConfig object is created first, and its constructor is createdtornado.web.Application()
And assignedself.Application
, Add@app.route
The modifier corresponds to the route object under RouterConfig. The Handler instance is assigned to the handler parameter, and the URL and Handler correspondence are added to the routing table, the attributes created by the URL in each Handler.
#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ # Created by ansheng on import tornadoimport tornado. webimport tornado. ioloopclass RouterConfig: def _ init _ (self): self. application = tornado. web. application () # create a route object def route (self, handler): self. application. add_handlers ('. * $ ', [(handler. URL, handler)]) # Add link ing to route table app = RouterConfig () # create route @ app. routeclass MainHandler (tornado. web. requestHandler): URL = R'/'def get (self, * args, ** kwargs): self. write ('hello, anshen') @ app. routeclass MainHandler (tornado. web. requestHandler): URL = R'/Hi' def get (self, * args, ** kwargs): self. write ('Hi, ansheng ') if _ name _ = "_ main _": app. application. listen (8000) print ("http: // 127.0.0.1: 8000/") tornado. ioloop. IOLoop. instance (). start ()
Version 2
Create a Route object and add the decorator to the Handler.@route(r'/')
And pass in the URL, which corresponds__call__
The url parameter in the method, and then add the route correspondence to the list in the form of a parent. After all the routes are added, create a Tornado path with an object, and then put the route table in, complete registration.
#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ # Created by ansheng on import tornado. ioloopimport tornado. webclass Route (object): "saves the relationship between each URL and Handler to a tuple and appends it to the list, the list contains all Handler "def _ init _ (self): self. urls = list () # route list def _ call _ (self, url, * args, ** kwargs): def register (cls): self. urls. append (url, cls) # Add the corresponding Route table to the route list. return cls return registerroute = Route () # create a route table object @ Route (R '/') class MainHandler (tornado. web. requestHandler): def get (self, * args, ** kwargs): self. write ('hello, anshen') @ route (R'/Hi') class MainHandler (tornado. web. requestHandler): def get (self, * args, ** kwargs): self. write ('Hi, anshen') application = tornado. web. application (route. urls) # create an app and add links to the Application object if _ name _ = '_ main _': application. listen (8000) print ("http: // 127.0.0.1: % s/" % 8000) tornado. ioloop. IOLoop. instance (). start ()
Version 3
This version is also used by me now. The principle is the same. The feature here is to inherit the Tornado route object.
#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ # Created by ansheng on import tornado. webimport tornado. ioloopclass RouterConfig (tornado. web. application): "resetting Tornado's built-in path objects" def route (self, url): ": param url: URL address: return: register the modifier "def register (handler):" ": param handler: Handler corresponding to URL: return: Handler" self. add_handlers (". * $ ", [(url, handler)]) # Add the URL and Handler ing to the routing table return handler return registerapp = RouterConfig (cookie_secret = 'ulb7beizmwpv545z ') # create a Tornado route object. The default route table is null @ app. route (R'/') class MainHandler (tornado. web. requestHandler): def get (self, * args, ** kwargs): self. write ('hello, anshen') @ app. route (R'/Hi') class MainHandler (tornado. web. requestHandler): def get (self, * args, ** kwargs): self. write ('Hi, ansheng ') if _ name _ = "_ main _": app. listen (8000) print ("http: // 127.0.0.1: % s/" % 8000) tornado. ioloop. IOLoop. instance (). start ()
Test
In the above versions, the test method and output are the same. You can use the requests module to simulate HTTP requests.
>>> Import requests >>> requests. get ('HTTP: // 127.0.0.1: 8000 /'). text 'hello, ansheng '>>> requests. get ('HTTP: // 127.0.0.1: 8000/Hi '). text 'Hi, anshen'
Summary
The above is all about automatically registering Tornado routes with the decorator. I hope the content in this article will help you in your study or work. If you have any questions, please leave a message.