This article mainly introduces how to automatically register Tornado routes using the decorator in python. three solutions are provided in this article. if you need them, let's take a look at them.
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'
For more details about how to automatically register Tornado routes with the decorator in python, please follow the PHP Chinese network!