Tornado there's no session inside? No, of course there is ~ I know there must be someone on GitHub to help me write it ~ O (∩_∩) o~
Thus, find the following project, using memcached to achieve the tornado session. The light will not be able to use, let us see how it is written ~
Project Address: Tornado-memcached-sessions
Let's start by looking at the demo ....
In app.py:
First notice that a new application class is defined, which inherits from Tornado.web.Application, where the application parameter settings is set in the initialization method of the class, and then the parent class and Session_ are initialized. Manager. (What is this?) For the moment, whatever it is ... )
Class Application (tornado.web.Application): def __init__ (self): settings = Dict (# set Cookie_secret, For Secure_cookie Cookie_secret = "e446976943b4e8442f099fed1f3fea28462d5832f483a0ed9a3d5d3859f==78d", # set Session_secret to generate session_id Session_secret = "3cdcb1f00803b6e78ab50b466a40b9977db396840c28307f428b25e2 277f1bcc ", # memcached address memcached_address = [" 127.0.0.1:11211 "], # Session Expiration Time Session_timeout = Template_path = Os.path.join (Os.path.dirname (__file__), "Templates"), stat Ic_path = Os.path.join (Os.path.dirname (__file__), "Static"), Xsrf_cookies = True, Login_url = "/logi n ",) handlers = [(R"/", MainHandler), (R"/login ", Loginhandler)] # Initial The parent class Tornado.web.Application tornado.web.application.__init__ (self, Handlers, **settings) # initializes the Session_ma of the class Nager Self.sessioN_manager = Session. SessionManager (settings["Session_secret"], settings["memcached_address"], settings["Session_timeout"])
In the following Loginhandler we can see the use of the session:
Class Loginhandler (Basehandler): def get (self): self.render ("login.html") def post (self): # Access self.session["user_name"] = self.get_argument ("name") in the form of a dictionary key-value pair # Modify the save to invoke the session, otherwise it will be equal to no modification oh ... Self.session.save () self.redirect ("/")
Is it very concise and clear from the point of view? So, careful you are not found now handler not inherit from Tornado.web.RequestHandler? With a strong Zuo (SI) Spirit we opened the base.py. Oh, my God, it's so short. (Oh, where do you want to go ...) )
The Basehandler method is simply initialized and overrides the Get_current_user method for user login verification.
Class Basehandler (Tornado.web.RequestHandler): def __init__ (self, *ARGC, **argkw): super (Basehandler, self) . __init__ (*ARGC, **argkw) # Defines the session of the handler, noting that each access initializes a session instance according to the HTTP feature Oh, it's important to understand what's behind you. Self.session = Session. Session (Self.application.session_manager, self) # What's this for? Used to verify login ... Please google about tornado.web.authenticated, is actually tornado provides user authentication def get_current_user (self): return Self.session.get ("user_name")
See here, is not contented? Oh, I finally understand! Hey, what's The Zuo (SI) spirit? The key is session.py Ah! You have a blank face back to the head ....
For the funeral, please listen to tell. (This style of writing is really good ....) )
Implementation of Tornado session support with memcached (i)