zoukankan      html  css  js  c++  java
  • tornado-cookies+pycket 验证

    1.pip install pycket

     pip install redis

    2.config

    settings = dict(
                debug=True,
                template_path='templates',
                static_path='static',       cookie_secret='secret',
                pycket={
                    'engine': 'redis',
                    'storage': {
                        'host': 'localhost',
                        'port': 6379,
                        'db_sessions': 2,
                        # 'password': '',
                        'db_notifications': 11,
                        'max_connections': 2 ** 31,
                    },
                    'cookies': {
                        'expires_days': 30,
                        'max_age': 5000
                    }
                },
            )

     3.构建BaseHandler

    from pycket.session import SessionMixin
    
    class BaseHandler(tornado.web.RequestHandler, SessionMixin):
        def get_current_user(self):
            return self.session.get('cookie_name')

    4.需要设置一个条件去验证,比如要求用户登录。就在用户登录的handler中设置一个cookie信息

    class LoginHandler(BaseHandler):
      def get(self):
         username = self.get_argument('username')
        passwd = self.get_argument('password')
        if username in db and passwd == db.passwd:
          self.sesion.set('cookie_name', value)
          self.write('success login')

    5.其他handler需要继承basehandler才能够使用装饰起@tornado.web.authenticated去验证用户是否登录

    class NoLoginNoShow(BaseHandler):
      @tornado.web.authenticated
      def get(self):
        self.write('this message is secrect'

    6.退出,可以通过self.session.delete('cookie_name')

    转载于:https://www.cnblogs.com/tangpg/p/9468724.html

  • 相关阅读:
    【面积并】 Atlantis
    【动态前k大 贪心】 Gone Fishing
    【复杂枚举】 library
    【双端队列bfs 网格图建图】拯救大兵瑞恩
    【奇偶传递关系 边带权】 奇偶游戏
    【权值并查集】 supermarket
    CF w4d3 A. Pythagorean Theorem II
    CF w4d2 C. Purification
    CF w4d2 B. Road Construction
    CF w4d2 A. Cakeminator
  • 原文地址:https://www.cnblogs.com/PMXGG/p/14314033.html
Copyright © 2011-2022 走看看