flask-session
Posted yafeng666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask-session相关的知识,希望对你有一定的参考价值。
flask的介绍以及使用
"""
要用flask-session,得先安装 pip install flask-session
最初我们村session是存在cookie中的,session大字典就是cookie中的val
那我门想把session存放到redis中,就必须使用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')
app.secret_key = 'alen is handsome'
#permanent=True,如果这个值为True,表示关闭浏览器,不失效,如果为Fasle,表示关闭浏览器,session就失效
#use_signer 默认是false,即可不需要秘钥,若改为true则需要配置秘钥
# 变成redis接口
app.session_interface = RedisSessionInterface(conn, key_prefix='alen', use_signer=True)
@app.route('/')
def hello_world():
session['name'] = 'alen'
return 'Hello World!'
@app.route('/index')
def index():
print(session['name'])
return 'ok'
if __name__ == '__main__':
app.run()
flask-session的常规写法
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_TYPE'] = Redis(host='127.0.0.1', port='6379', password='Admin123') # 把连接写在配置中
Session(app) # Session内部实现了接口连接
@app.route("/")
def index():
session['name'] = "alen"
return "ok"
@app.route("/index")
def index1():
print(session['name'])
return "ok"
if __name__ == '__main__':
app.run()
flask-session的Session源码
def _get_interface(self, app):
config = app.config.copy()
config.setdefault('SESSION_TYPE', 'null')
config.setdefault('SESSION_PERMANENT', True)
config.setdefault('SESSION_USE_SIGNER', False)
config.setdefault('SESSION_KEY_PREFIX', 'session:')
config.setdefault('SESSION_REDIS', None)
config.setdefault('SESSION_MEMCACHED', None)
config.setdefault('SESSION_FILE_DIR',
os.path.join(os.getcwd(), 'flask_session'))
config.setdefault('SESSION_FILE_THRESHOLD', 500)
config.setdefault('SESSION_FILE_MODE', 384)
config.setdefault('SESSION_MONGODB', None)
config.setdefault('SESSION_MONGODB_DB', 'flask_session')
config.setdefault('SESSION_MONGODB_COLLECT', 'sessions')
config.setdefault('SESSION_SQLALCHEMY', None)
config.setdefault('SESSION_SQLALCHEMY_TABLE', 'sessions')
if config['SESSION_TYPE'] == 'redis':
session_interface = RedisSessionInterface(
config['SESSION_REDIS'], config['SESSION_KEY_PREFIX'],
config['SESSION_USE_SIGNER'], config['SESSION_PERMANENT'])
elif config['SESSION_TYPE'] == 'memcached':
session_interface = MemcachedSessionInterface(
config['SESSION_MEMCACHED'], config['SESSION_KEY_PREFIX'],
config['SESSION_USE_SIGNER'], config['SESSION_PERMANENT'])
elif config['SESSION_TYPE'] == 'filesystem':
session_interface = FileSystemSessionInterface(
config['SESSION_FILE_DIR'], config['SESSION_FILE_THRESHOLD'],
config['SESSION_FILE_MODE'], config['SESSION_KEY_PREFIX'],
config['SESSION_USE_SIGNER'], config['SESSION_PERMANENT'])
elif config['SESSION_TYPE'] == 'mongodb':
session_interface = MongoDBSessionInterface(
config['SESSION_MONGODB'], config['SESSION_MONGODB_DB'],
config['SESSION_MONGODB_COLLECT'],
config['SESSION_KEY_PREFIX'], config['SESSION_USE_SIGNER'],
config['SESSION_PERMANENT'])
elif config['SESSION_TYPE'] == 'sqlalchemy':
session_interface = SqlAlchemySessionInterface(
app, config['SESSION_SQLALCHEMY'],
config['SESSION_SQLALCHEMY_TABLE'],
config['SESSION_KEY_PREFIX'], config['SESSION_USE_SIGNER'],
config['SESSION_PERMANENT'])
else:
session_interface = NullSessionInterface()
return session_interface
以上是关于flask-session的主要内容,如果未能解决你的问题,请参考以下文章