欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tornado 登录状态保持

程序员文章站 2022-07-15 12:13:15
...
1. pip 安装pycket 和 redis

pycket: tornado中配置redis信息以及处理session信息

2. 登录页面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link rel="stylesheet" href="http://at.alicdn.com/t/font_766584_c92pmac2r1k.css">
    <style>
        body {
            background: #c1c1c1;
        }

        .contain {
            width: 100%;
            padding-top: 50px;
        }

        .contain .login-contain {
            width: 400px;
            margin: auto;
            background: white;
            padding: 0 20px;
        }

        .contain .login-contain .login-top {
            line-height: 60px;
            height: 60px;
            border-bottom: 1px solid #ddd;
        }

        .contain .login-contain .login-top h2 {
            float: left;
            border-bottom: 5px solid blue;
            height: 60px;
            box-sizing: border-box;
        }

        .contain .login-contain .login-top a {
            float: right;
        }

        .contain .login-contain form div {
            margin-top: 20px;
        }

        .contain .login-contain form div input {
            width: 100%;
            height: 50px;
            padding-left: 10px;
            box-sizing: border-box;
        }

        .contain .login-contain form div label input {
            width: 15px;
            height: 15px;
            vertical-align: -2px;
        }

        .contain .login-contain form div a {
            float: right;
            color: #dddddd;
        }

        .contain .login-contain form div input[type=submit] {
            background: skyblue;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>

<!--main star-->
<div class="contain">
    <div class="login-contain">
        <div class="login-top">
            <h2>请登录</h2>
{#            <a href="reg.html">立即注册&gt;</a>#}
        </div>
        <form action="/login" method="post">
            <div><input type="text" placeholder="请输入你的账号" name="username"></div>
            <div><input type="password" placeholder="请输入你的密码" name="password"></div>
            <div>
                <label><input type="checkbox">七天内免登录</label>
                <a href="javascript:void(0);">忘记密码</a>
            </div>
            <div><input type="submit" value="登录"></div>
        </form>
    </div>
</div>

<script>

</script>
</body>
</html>
3. web 服务

import tornado.web
import tornado.options
from tornado.options import define, options
from tornado.web import RequestHandler
import tornado.httpserver
import tornado.ioloop

from pycket.session import SessionMixin

define('port', default=8080, help='run port', type=int)


class LoginHandler(RequestHandler, SessionMixin):
    def get(self):
        self.render('login.html')

    def post(self):
        username = self.get_argument('username')
        password = self.get_argument('password')
        # 保存cookie信息到redis数据库
        self.session.set('username', username)
        # 获取cookie信息
        print(self.session.get('username'))

        self.write('<div>用户名: {}</div>'.format(username) + '<div>密码: {}</div>'.format(password))


class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r'/login', LoginHandler),

        ]
        settings = dict(
            debug=True,
            template_path='template',
            static_path='static',

            # 加密cookie
            cookie_secret='qwe123',
            # redis配置
            pycket={
                'engine': 'redis',
                'storage': {
                    'host': 'localhost',
                    'port': 6379,
                    # redis 第5个库
                    'db_sessions': 5,
                    'max_connections': 2 ** 30
                },
                'cookies': {
                    # 设置cookie过期时间
                    'expires_days': 30
                }
            }

        )
        super(Application, self).__init__(handlers, **settings)

app = Application()


if __name__ == '__main__':
    tornado.options.parse_command_line()
    print('监听端口: ', options.port)
    server = tornado.httpserver.HTTPServer(app)
    server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

4. 页面显示

tornado 登录状态保持

5. 模拟登录

tornado 登录状态保持
tornado 登录状态保持

6. 登录后可查看cookie和session信息;过期时间为30天(web服务里自己设置的过期时间);cookie保存在浏览器,session保存在redis里

tornado 登录状态保持

tornado 登录状态保持

相关标签: tornado