首页 > 编程 > Python > 正文

Flask之flask-session的具体使用

2020-02-15 22:17:03
字体:
来源:转载
供稿:网友

flask-session是flask框架的session组件,由于原来flask内置session使用签名cookie保存,该组件则将支持session保存到多个地方,如:

redis memcached filesystem mongodb sqlalchmey

安装

pip3 install flask-session 

存储方式

redis

#!/usr/bin/env python# -*- coding:utf-8 -import redisfrom flask import Flask, sessionfrom flask_session import Session app = Flask(__name__)app.debug = Trueapp.secret_key = 'xxxx' app.config['SESSION_TYPE'] = 'redis' # session类型为redisapp.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='123123') # 用于连接redis的配置 Session(app) @app.route('/index')def index():  session['k1'] = 'v1'  return 'xx' if __name__ == '__main__':  app.run() 

memcached

#!/usr/bin/env python# -*- coding:utf-8 -import redisfrom flask import Flask, sessionfrom flask_session import Sessionimport memcache app = Flask(__name__)app.debug = Trueapp.secret_key = 'xxxx' app.config['SESSION_TYPE'] = 'memcached' # session类型为redisapp.config['SESSION_PERMANENT'] = True # 如果设置为True,则关闭浏览器session就失效。app.config['SESSION_USE_SIGNER'] = False # 是否对发送到浏览器上session的cookie值进行加密app.config['SESSION_KEY_PREFIX'] = 'session:' # 保存到session中的值的前缀app.config['SESSION_MEMCACHED'] = memcache.Client(['10.211.55.4:12000'])  Session(app)  @app.route('/index')def index():  session['k1'] = 'v1'  return 'xx' if __name__ == '__main__':  app.run() 

filesystem

#!/usr/bin/env python# -*- coding:utf-8 -import redisfrom flask import Flask, sessionfrom flask_session import Session app = Flask(__name__)app.debug = Trueapp.secret_key = 'xxxx' app.config['SESSION_TYPE'] = 'filesystem' # session类型为redisapp.config[  'SESSION_FILE_DIR'] = '/Users/wupeiqi/PycharmProjects/grocery/96.Flask新课程/组件/2.flask-session' # session类型为redisapp.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('/index')def index():  session['k1'] = 'v1'  session['k2'] = 'v1'  return 'xx' if __name__ == '__main__':  app.run() 

mongodb

#!/usr/bin/env python# -*- coding:utf-8 -import redisfrom flask import Flask, sessionfrom flask_session import Sessionimport pymongoapp = Flask(__name__)app.debug = Trueapp.secret_key = 'xxxx' app.config['SESSION_TYPE'] = 'mongodb' # session类型为redis app.config['SESSION_MONGODB'] = pymongo.MongoClient()app.config['SESSION_MONGODB_DB'] = 'mongo的db名称(数据库名称)'app.config['SESSION_MONGODB_COLLECT'] = '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('/index')def index():  session['k1'] = 'v1'  session['k2'] = 'v1'  return 'xx' if __name__ == '__main__':  app.run()             
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表