1. 程式人生 > >為Tornado框架加上基於Redis或Memcached的session 【第三方】

為Tornado框架加上基於Redis或Memcached的session 【第三方】

clas secret () res sdh cti utf nec 安裝

Tornado 沒有session,只有cookie_secret,這在一些情況下是不利於開發的。所有我們可以給Tornado加上session的功能。

這篇文章講解的是依靠第三方包來實現。以後的文章我們還可以自己寫一套。

以下為步驟:

1,安裝包 pycket

$ pip install pycket

2,使用時引入包

from pycket.session import SessionMixin

3,通過Handler傳入(還可以創建一個BaseHandler,將其封裝到BaseHandler也可以。這裏為了簡單,就不封裝了)

4,在配置文件中進行配置

‘‘‘
        Redis 配置
        
‘‘‘ # settings = { # # cookie_secret必須設置 # ‘cookie_secret‘: "2379874hsdhf0234990sdhsaiuofyasop977djdj", # ‘xsrf_cookies‘: True, # ‘debug‘: False, # # 1 配置pycket 註意別忘記開啟redis服務 # ‘pycket‘: { # ‘engine‘: ‘redis‘,
# ‘storage‘: { # ‘host‘: ‘localhost‘, # ‘port‘: 6379, # ‘db_sessions‘: 10, # ‘db_notifications‘: 11, # ‘max_connections‘: 2 ** 31, # }, # ‘cookies‘: { #
# 設置過期時間 # ‘expires_days‘: 2, # #‘expires‘:None, #秒 # }, # } # } ‘‘‘ Memcached 配置 ‘‘‘ settings = { cookie_secret: "2379874hsdhf0234990sdhsaiuofyasop977djdj", pycket: { engine: memcached, storage: { servers: (localhost:11211,) }, cookies: { expires_days: 120, }, }, }

5,使用

    def get(self):
        # 兩種設置方式
        self.session.set(foo, [bar, baz])
        self.session[test] = test!

        # 兩種獲取方式
        print self.session[test]
        print self.session.get(foo)

完整代碼:

技術分享
 1 #--*--coding:utf-8--*--
 2 
 3 import tornado.web
 4 import tornado.httpserver
 5 import tornado.ioloop
 6 import tornado.options
 7 import os.path
 8 from pycket.session import SessionMixin
 9 from tornado.options import define, options
10 define("port", default=8001, help="run on the given port", type=int)
11 
12 
13 class Application(tornado.web.Application):
14     def __init__(self):
15         handlers = [
16             (r"/", MainHandler),
17         ]
18         ‘‘‘
19         Redis 配置
20         ‘‘‘
21         # settings = {
22         #     # cookie_secret必須設置
23         #     ‘cookie_secret‘: "2379874hsdhf0234990sdhsaiuofyasop977djdj",
24         #     ‘xsrf_cookies‘: True,
25         #     ‘debug‘: False,
26         #     # 1 配置pycket 註意別忘記開啟redis服務
27         #     ‘pycket‘: {
28         #         ‘engine‘: ‘redis‘,
29         #         ‘storage‘: {
30         #             ‘host‘: ‘localhost‘,
31         #             ‘port‘: 6379,
32         #             ‘db_sessions‘: 10,
33         #             ‘db_notifications‘: 11,
34         #             ‘max_connections‘: 2 ** 31,
35         #         },
36         #         ‘cookies‘: {
37         #             # 設置過期時間
38         #             ‘expires_days‘: 2,
39         #             #‘expires‘:None, #秒
40         #         },
41         #     }
42         # }
43         ‘‘‘
44         Memcached 配置
45         ‘‘‘
46         settings = {
47             cookie_secret: "2379874hsdhf0234990sdhsaiuofyasop977djdj",
48             pycket: {
49                 engine: memcached,
50                 storage: {
51                     servers: (localhost:11211,)
52                 },
53                 cookies: {
54                     expires_days: 120,
55                 },
56             },
57         }
58         tornado.web.Application.__init__(self, handlers, **settings)
59 
60 
61 class MainHandler(tornado.web.RequestHandler, SessionMixin):
62     def get(self):
63         # 兩種設置方式
64         self.session.set(foo, [bar, baz])
65         self.session[test] = test!
66 
67         # 兩種獲取方式
68         print self.session[test]
69         print self.session.get(foo)
70 
71 if __name__ == "__main__":
72     tornado.options.parse_command_line()
73     http_server = tornado.httpserver.HTTPServer(Application())
74     http_server.listen(options.port)
75     tornado.ioloop.IOLoop.instance().start()
View Code

註意:若使用Redis,確保你的Redis已經啟動。若使用 Memcached ,請確保Memcached 已經啟動

為Tornado框架加上基於Redis或Memcached的session 【第三方】