flask-session是flask框架的session组件,由于原来flask内置session使用签名cookie保存,该组件则将支持session保存到多个地方
一、filesystem
from flask import Flask, session from flask_session import Session app = Flask(__name__) app.secret_key = "Yang" app.config['SESSION_TYPE'] = 'filesystem' # session类型为文件 app.config['SESSION_FILE_DIR'] = r'C:\\Users\\25754\\Desktop\\session' # session保存路径 app.config['SESSION_FILE_THRESHOLD'] = 500 # 存储session的个数如果大于这个值时,就要开始进行删除了 app.config['SESSION_FILE_MODE'] = 384 # 文件权限类型 app.config['SESSION_PERMANENT'] = True # 如果设置为True,则关闭浏览器session就失效。 app.config['SESSION_USE_SIGNER'] = False # 是否对发送到浏览器上session的cookie值进行加密 app.config['SESSION_KEY_PREFIX'] = 'session:' # 保存到session中的值的前缀 Session(app) @app.route('/') def index(): session['username'] = 'yang' return 'index' @app.route('/session') def get_session(): return session['username'] if __name__ == '__main__': app.run(debug=True)
二、redis
from flask import Flask, session from flask_session import Session import redis app = Flask(__name__) app.debug = True app.secret_key = 'xxxx' app.config['SESSION_TYPE'] = 'redis' # session类型为redis app.config['SESSION_PERMANENT'] = False # 如果设置为True,则关闭浏览器session就失效。 app.config['SESSION_USE_SIGNER'] = False # 是否对发送到浏览器上session的cookie值进行加密 app.config['SESSION_KEY_PREFIX'] = 'session:' # 保存到session中的值的前缀 app.config['SESSION_REDIS'] = redis.Redis(host='127.0.0.1', port='6379', password='') # 用于连接redis的配置 Session(app) @app.route('/') def index(): session['username'] = 'yang' return 'index' @app.route('/session') def get_session(): return session['username'] if __name__ == '__main__': app.run()
三、mongodb
from flask import Flask, session from flask_session import Session import pymongo app = Flask(__name__) app.debug = True app.secret_key = 'xxxx' app.config['SESSION_TYPE'] = 'mongodb' # session类型为mongodb app.config['SESSION_MONGODB'] = pymongo.MongoClient() app.config['SESSION_MONGODB_DB'] = 'db0' # mongo的db名称(数据库名称) app.config['SESSION_MONGODB_COLLECT'] = 'session' # mongo的collect名称(表名称) app.config['SESSION_PERMANENT'] = True # 如果设置为True,则关闭浏览器session就失效。 app.config['SESSION_USE_SIGNER'] = False # 是否对发送到浏览器上session的cookie值进行加密 app.config['SESSION_KEY_PREFIX'] = 'session:' # 保存到session中的值的前缀 Session(app) @app.route('/') def index(): session['username'] = 'yang' return 'index' @app.route('/session') def get_session(): return session['username'] if __name__ == '__main__': app.run()
四、sqlalchemy
表结构
from flask import Flask, session from flask_session import Session from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.debug = True app.secret_key = 'xxxx' # 设置数据库链接 app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://test01:[email protected]:3306/test01?charset=utf8' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True # 实例化SQLAlchemy db = SQLAlchemy(app) app.config['SESSION_TYPE'] = 'sqlalchemy' # session类型为sqlalchemy app.config['SESSION_SQLALCHEMY'] = db # SQLAlchemy对象 app.config['SESSION_SQLALCHEMY_TABLE'] = 'session' # session要保存的表名称 app.config['SESSION_PERMANENT'] = True # 如果设置为True,则关闭浏览器session就失效。 app.config['SESSION_USE_SIGNER'] = False # 是否对发送到浏览器上session的cookie值进行加密 app.config['SESSION_KEY_PREFIX'] = 'session:' # 保存到session中的值的前缀 Session(app) @app.route('/') def index(): session['username'] = 'yang' return 'index' @app.route('/session') def get_session(): return session['username'] if __name__ == '__main__': app.run()
标签:session,__,框架,Flask,app,SESSION,config From: https://www.cnblogs.com/yang-2018/p/17298752.html