标签:__name__ config emc def 密钥 mongo col interface name
作用:将默认保存的签名cookie中的值 保存到 redis/memcached/file/Mongodb/SQLAlchemy
安装:pip3 install flask-session
from flask import Flask,session from flask_session import RedisSessionInterface import redis app = Flask(__name__) conn = redis.Redis(host=‘127.0.0.1‘,port=6379,password="Admin123",db=4) # use_signer设置为True,就必须要写密钥,为False就不需要。默认为False # permanent设置为True如果为表示关闭浏览器不失效,默认为True app.session_interface = RedisSessionInterface(conn,key_prefix="xxx",use_signer=True,permanent=True) @app.route("/") def hello_world(): session[‘name‘] = ‘xxx‘ return "hello world" @app.route("/index") def index(): print(session[‘name‘]) return "ok" if __name__ == ‘__main__‘: app.run()
from redis import Redis from flask import Flask,session from flask_session import Session app = Flask(__name__) app.config[‘SESSION_TYPE‘] = ‘redis‘ app.config[‘SESSION_REDIS‘] = Redis(host=‘127.0.0.1‘,port=‘6379‘,password="Admin123") Session(app) @app.route("/") def index(): session[‘name‘] = "tank" return "ok" @app.route("/index") def index1(): print(session[‘name‘]) return "ok" if __name__ == ‘__main__‘: app.run()
标签:__name__ config emc def 密钥 mongo col interface name
原文地址:https://www.cnblogs.com/baohanblog/p/12536430.html