Add Redis or memcached-based session "third party" to the Tornado framework

Source: Internet
Author: User
Tags memcached

Tornado no session, only Cookie_secret, which in some cases is not conducive to development. All we can do is add the function of session to tornado.

This article is about relying on third-party packages to implement. Later articles we can also write a set of their own.

Here are the steps:

1, installation package Pycket

$ pip Install Pycket

2, introducing the package when used

 from Import Sessionmixin

3, passed through handler (you can also create a basehandler and encapsulate it to basehandler. Here for simplicity, it is not encapsulated)

4, configuring in the configuration file

" "Redis Configuration" "        #settings = {        ## Cookie_secret must be set        #' Cookie_secret ': "2379874hsdhf0234990sdhsaiuofyasop977djdj",        #' xsrf_cookies ': True,        #' Debug ': False,        ## 1 Configuration pycket Note Don't forget to turn on Redis services        #' Pycket ': {        #' engine ': ' Redis ',        #' storage ': {        #' host ': ' localhost ',        #' Port ': 6379,        #' db_sessions ': ten,        #' db_notifications ': One by one,        #' max_connections ': 2 * *,        #         },        #' cookies ': {        ## Set Expiration Time        #' expires_days ': 2,        ## ' expires ': None, #秒        #         },        #     }        # }        " "Memcached Configuration" "Settings= {            'Cookie_secret':"2379874HSDHF0234990SDHSAIUOFYASOP977DJDJ",            'Pycket': {                'engine':'memcached',                'Storage': {                    'Servers': ('localhost:11211',)                },                'Cookies': {                    'expires_days': 120,                },            },        }

5, using

    defGet (self):#two ways to set upSelf.session.set ('Foo', ['Bar','Baz']) self.session['Test'] ='test!'        #two ways to get there        Printself.session['Test']        PrintSelf.session.get ('Foo')

Full code:

1 #--*--coding:utf-8--*--2 3 ImportTornado.web4 ImportTornado.httpserver5 ImportTornado.ioloop6 Importtornado.options7 ImportOs.path8  fromPycket.sessionImportsessionmixin9  fromTornado.optionsImportDefine, OptionsTenDefine"Port", default=8001, help="run on the given port", type=int) One  A  - classApplication (tornado.web.Application): -     def __init__(self): theHandlers = [ -(r"/", MainHandler), -         ] -         " " + Redis Configuration -         " " +         #settings = { A         ## Cookie_secret must be set at         #' Cookie_secret ': "2379874hsdhf0234990sdhsaiuofyasop977djdj", -         #' xsrf_cookies ': True, -         #' Debug ': False, -         ## 1 Configuration pycket Note Don't forget to turn on Redis services -         #' Pycket ': { -         #' engine ': ' Redis ', in         #' storage ': { -         #' host ': ' localhost ', to         #' Port ': 6379, +         #' db_sessions ': ten, -         #' db_notifications ': One by one, the         #' max_connections ': 2 * *, *         #         }, $         #' cookies ': {Panax Notoginseng         ## Set Expiration Time -         #' expires_days ': 2, the         ## ' expires ': None, #秒 +         #         }, A         #     } the         # } +         " " - Memcached Configuration $         " " $Settings = { -             'Cookie_secret':"2379874HSDHF0234990SDHSAIUOFYASOP977DJDJ", -             'Pycket': { the                 'engine':'memcached', -                 'Storage': {Wuyi                     'Servers': ('localhost:11211',) the                 }, -                 'Cookies': { Wu                     'expires_days': 120, -                 }, About             }, $         } -Tornado.web.Application.__init__(Self, handlers, * *settings) -  -  A classMainHandler (Tornado.web.RequestHandler, sessionmixin): +     defGet (self): the         #two ways to set up -Self.session.set ('Foo', ['Bar','Baz']) $self.session['Test'] ='test!' the  the         #two ways to get there the         Printself.session['Test'] the         PrintSelf.session.get ('Foo') -  in if __name__=="__main__": the Tornado.options.parse_command_line () theHttp_server =tornado.httpserver.HTTPServer (Application ()) About Http_server.listen (Options.port) theTornado.ioloop.IOLoop.instance (). Start ()
View Code

Note: If you use Redis, make sure your redis is up. If you use Memcached, make sure Memcached is started

Add Redis or memcached-based session "third party" to the Tornado framework

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.