• tornado cookie和session


    一、cookie

    (一).cookie运作机制

    (二).设置cookie的常用方法

    self.set_cookie('cookie_test','this_is_test') 默认过期时间是浏览器关闭会话时

    self.set_cookie('cookie_test1','this_is_test',expires=time.time()+60) 设置过期时间为60秒

    self.set_cookie('cookie_test2','this_is_test',expires_days=1) 设置过期时间为一天

    self.set_cookie('cookie_test3','this_is_test',path='/') 设置路径

    self.set_cookie('cookie_test4','this_is_test',httponly=True) 设置JS不可以获取cookie

    self.set_cookie('cookie_test5','this_is_test',max_age=120,expires=time.time()+60) max_age优先级高

    self.set_secure_cookie('cookie_test6','this_is_test') 设置加密cookie,需要先设置application的cookie_secret参数

    (三).获取cookie

    (1).获取一般的cookie

    self.get_cookie('cookie_test')

    (2).获取加密cookie

    self.get_secure_cookie('cookie_test6')

    (四).使用cookie进行登录验证

    (1).导入装饰器

    from tornado.web import authenticated

    (2).声明BaseHandler

    class BaseHandler(tornado.web.RequestHandler):
        def get_current_user(self):
            current_user = self.get_secure_cookie('ID')
    
            if current_user:
                return current_user
            
            return None
    View Code

    (3).在application中设置login的值(登录的路由)

    login_url='/login',

    (4).装饰需要登录验证的请求

    class BuyHandler(BaseHandler):
        @authenticated
        def get(self):
            self.write('BuyHandler')
    View Code

    (5).获取之前路由

    class LoginHandler(BaseHandler):
        def get(self):
            nextname = self.get_argument('next', '')
            self.render('01in_out.html', nextname=nextname)
    View Code

    在装饰了@authenticated之后,如果验证不成功,会自动跳转到登录路由,并且在URL后面加上next参数,next参数的值就是之前的路由

    (6).修改模板文件

    <form method="post" action="/login?next={{ nextname }}"></form>
    View Code

    (7).修改post方法

    def post(self, *args, **kwargs):
        nextname = self.get_argument('next', '')
        user = self.get_argument('name', '')
        username = User.by_name(user)
        passwd = self.get_argument('password', '')
    
        if username and passwd == username.password:
            self.set_secure_cookie('ID', username.username, max_age=100)
            self.redirect(nextname)
        else:
            self.render('01in_out.html', nextname=nextname)
    View Code

    获取之前的页面的路由。当登录验证通过之后,设置加密的cookie,并跳转到之前的路由。

    二、session

    在服务端,需要借助redis进行保存信息

    (一).安装模块

    pip install pycket

    pip install redis

    (二).导入模块

    from pycket.session import SessionMixin

    (三).在application中添加配置

    pycket = {
                 'engine': 'redis',
                 'storage': {
                     'host': 'localhost',
                     'port': 6379,
                     'db_sessions': 5,
                     'db_notifications': 11,
                     'max_connections': 2 ** 31,
                 },
                 'cookies': {
                     'expires_days': 30,
                     'max_age': 100
                 },
             },
    View Code

    (四).继承SessionMixin

    class BaseHandler(tornado.web.RequestHandler, SessionMixin):pass

    (五).设置session

    self.session.set('user', username[0].username)

    (六).获取session

    current_user = self.session.get('user')

    三、XSRF

    加入XSRF是为了防范提交form表单时遭到攻击者的攻击。

    tornado有内建的XSRF防范机制,要使用此机制,只需要在模板中添加如下代码:

    {% module xsrf_form_html() %}

  • 相关阅读:
    HDU 1124 Factorial(简单数论)
    29.QT主窗口加widget
    28.开始画面和异形窗口
    27.Qt时钟
    26.QT颜色与布局
    25.QT进度条
    146.正则表达式
    24.qint64转QString 以及获取文件属性
    23.QFile遍历
    22.监视文件
  • 原文地址:https://www.cnblogs.com/quanquan616/p/9614169.html
Copyright © 2020-2023  润新知