当前位置: 首页 > 工具软件 > flask-session > 使用案例 >

flask-session组件

周翰
2023-12-01

  flask内置session使用签名cookie保存,flask-session 组件则将支持session保存到多个地方:

1.redis:
2.memcached :
3.filesystem:
4.mongodb :
5.sqlalchmey:

应用程序比较小,用原生的加密ccokie 保存session(内置)

应用程序比较大,可以用redis(flask-session)
安装 :pip3 install flask-session

1.redis:

from redis import StrictRedis

from flask import Flask, session

from flask_session import Session

app = Flask(__name__)

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['PERMANENT_SESSION_LIFETIME'] = 3600 #手动指定有效期

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(debug = True)

2.memcached:

from redis import StrictRedis

from flask import Flask, session

from flask_session import Session

import memcache

app = Flask(__name__)

app.secret_key = 'xxxx'

app.config['SESSION_TYPE'] = 'memcached' 

app.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(debug = True)

3.filesystem:

from redis import StrictRedis

from flask import Flask, session

from flask_session import Session

app = Flask(__name__)

app.secret_key = 'xxxx'

app.config['SESSION_TYPE'] = 'filesystem'  

app.config[ 'SESSION_FILE_DIR'] = '/Users/wupeiqi/PycharmProjects/grocery/96.Flask新课程/组件/2.flask-session'  # session类型为redis

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('/index')

def index():

    session['k1'] = 'v1'

    session['k2'] = 'v1'

    return 'xx'

if __name__ == '__main__':

    app.run(debug = True)

4.mongodb

from redis import StrictRedis

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'  

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(debug = True)

5.sqlalchemy

from redis import StrictRedis

from flask import Flask, session

from flask_session import Session as FSession

from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.secret_key = 'xxxx'

# 设置数据库链接

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:mysql@localhost/python'# 指定连接的数据库,数据库必须要手动创建
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False# 动态追踪修改的配置,如果设置为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中的值的前缀

FSession(app)

@app.route('/index')

def index():

    session['k1'] = 'v1'

    session['k2'] = 'v1'

    return 'xx'

if __name__ == '__main__':

    app.run(debug = True)

 类似资料: