python automatically registers Tornado routing details with decorator

  • 2020-05-26 09:28:54
  • OfStack

Version 1

In this version, the RouterConfig object is first created and its constructor is created tornado.web.Application() And assignment self.Application , on each Handler @app.route The decorator corresponds to the route object below RouterConfig, where the Handler instance will be assigned to the handler parameter, and finally the URL and Handler corresponding relationship will be added to the routing table. URL creates the properties in each Handler.


#!/usr/bin/env python
# _*_ coding:utf-8 _*_

# Created by  A bird  on 2017/2/9

import tornado
import tornado.web
import tornado.ioloop

class RouterConfig:
 def __init__(self):
  self.Application = tornado.web.Application() #  Creating a routing object 

 def route(self, handler):
  self.Application.add_handlers('.*$', [(handler.URL, handler)]) #  A path relational map is added to the routing table 

app = RouterConfig() #  Create routing 

@app.route
class MainHandler(tornado.web.RequestHandler):
 URL = r'/'

 def get(self, *args, **kwargs):
  self.write('Hello,  A bird ')

@app.route
class MainHandler(tornado.web.RequestHandler):
 URL = r'/hi'

 def get(self, *args, **kwargs):
  self.write('hi,  A bird ')

if __name__ == "__main__":
 app.Application.listen(8000)
 print("http://127.0.0.1:8000/")
 tornado.ioloop.IOLoop.instance().start()

Version 2

Create the Route object, then add the decorator to Handler @route(r'/') , and pass in URL, which corresponds to __call__ The url parameter in the method, and then the routing correspondence is added to the list in the way of a meta-ancestor. After all the routes are added, the path of Tornado is created with an object, and then the routing table is put in, and finally the registration is completed.


#!/usr/bin/env python
# _*_ coding:utf-8 _*_

# Created by  A bird  on 2017/2/9

import tornado.ioloop
import tornado.web

class Route(object):
 """  Each of the URL with Handler The relationship is saved to 1 And then append it to the list, which contains all of them Handler """

 def __init__(self):
  self.urls = list() #  The routing list 

 def __call__(self, url, *args, **kwargs):
  def register(cls):
   self.urls.append((url, cls)) #  Adds the corresponding relationship table for the route to the route list 
   return cls

  return register

route = Route() #  Create the routing table object 

@route(r'/')
class MainHandler(tornado.web.RequestHandler):
 def get(self, *args, **kwargs):
  self.write('Hello,  A bird ')

@route(r'/hi')
class MainHandler(tornado.web.RequestHandler):
 def get(self, *args, **kwargs):
  self.write('hi,  A bird ')

application = tornado.web.Application(route.urls) #  create app And put the road into the relationship Application In the 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 the version I am using now, the principle is the same, here the characteristic is the inheritance Tornado routing object


#!/usr/bin/env python
# _*_ coding:utf-8 _*_

# Created by  A bird  on 2017/2/9

import tornado.web
import tornado.ioloop

class RouterConfig(tornado.web.Application):
 """  reset Tornado Your own path has an object  """

 def route(self, url):
  """
  :param url: URL address 
  :return:  Register the decorator for the corresponding table for the routing relationship 
  """

  def register(handler):
   """
   :param handler: URL The corresponding Handler
   :return: Handler
   """
   self.add_handlers(".*$", [(url, handler)]) # URL and Handler Correspondence is added to the routing table 
   return handler

  return register

app = RouterConfig(cookie_secret='ulb7bEIZmwpV545Z') #  create Tornado Routing object. The default routing table is empty 

@app.route(r'/')
class MainHandler(tornado.web.RequestHandler):
 def get(self, *args, **kwargs):
  self.write('Hello,  A bird ')

@app.route(r'/hi')
class MainHandler(tornado.web.RequestHandler):
 def get(self, *args, **kwargs):
  self.write('hi,  A bird ')

if __name__ == "__main__":
 app.listen(8000)
 print("http://127.0.0.1:%s/" % 8000)
 tornado.ioloop.IOLoop.instance().start()

test

In the above version, the test method and output are all one-like, and the requests module can be used to simulate the HTTP request


>>> import requests
>>> requests.get('http://127.0.0.1:8000/').text
'Hello,  A bird '
>>> requests.get('http://127.0.0.1:8000/hi').text
'hi,  A bird '

conclusion

The above is all about using decorator to automatically register Tornado route, I hope the content of this article can bring you a certain help in your study or work, if you have any questions, you can leave a message to communicate.


Related articles: