pip install flask-session
from flask import Flask, session
from flask_script import Manager
from redis import Redis
from flask_session import Session
app = Flask(name)
app.config['SECRET_KEY'] = '123456'
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_REDIS'] = Redis()
manager = Manager(app)
sess = Session(app)
# 可以通过两步进行初始化
# sess.init_app(app)
@app.route('/')
def index():
return 'flask-session'
@app.route('/get/')
def get_session():
return session.get('name', '你是谁?')
@app.route('/set/')
def set_session():
session['name'] = 'dahua'
return 'session已设置'
if __name__ == '__main__':
manager.run()