flash-session

作用:更改session存储的位置

1、session默认存放在浏览器的cookie中

源码

wsgi->app.__call__->wsgi_app->push->self.app.session_interface->session_interface = SecureCookieSessionInterface()->open_session和save_session

2、更改session储存的位置

a、下载

pip3 install -i https://pypi.douban.com/simple flask-session

b、源码分析

Session->self.init_app(app)->app.session_interface = self._get_interface(app)->session_interface

other

MemcachedSessionInterface
FileSystemSessionInterface
MongoDBSessionInterface
SqlAlchemySessionInterface

具体看源码

c、示例

import redis
from flask import Flask, session
# 1.导入模块
from flask_session import Session
# from flask.sessions import SecureCookieSession  # 原始
# from flask_session import RedisSessionInterface # 修改
app = Flask(__name__)

# app.session_interface = SecureCookieSessionInterface()
# app.session_interface = RedisSessionInterface()
# 2.修改配置文件
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_REDIS'] = redis.Redis(host='ip', port=6379, password='密码')
# 3.注册
Session(app)


@app.route('/login')
def login():
    session['user'] = 'alex'
    return 'Hello'


@app.route('/home')
def index():
    print(session.get('user'))

    return 'World'


if __name__ == '__main__':
    app.run()

 

posted @ 2019-09-28 23:11  市丸银  阅读(268)  评论(0编辑  收藏  举报