簡(jiǎn)介
flask-session是flask框架的session組件疚鲤,由于原來(lái)flask內(nèi)置session使用簽名cookie保存,該組件則將支持session保存到多個(gè)地方缘挑,如:
- redis
- memcached
- filesystem
- mongodb
- sqlalchmey
安裝
pip3 install flask-session
存儲(chǔ)方式
redis
#!/usr/bin/env python
# -*- coding:utf-8 -
import redis
from flask import Flask, session
from flask_session import Session
app = Flask(__name__)
app.debug = True
app.secret_key = 'xxxx'
app.config['SESSION_TYPE'] = 'redis' # session類型為redis
app.config['SESSION_PERMANENT'] = False # 如果設(shè)置為T(mén)rue集歇,則關(guān)閉瀏覽器session就失效。
app.config['SESSION_USE_SIGNER'] = False # 是否對(duì)發(fā)送到瀏覽器上session的cookie值進(jìn)行加密
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 -
from flask import Flask, session
from flask_session import Session
import memcache
app = Flask(__name__)
app.debug = True
app.secret_key = 'xxxx'
app.config['SESSION_TYPE'] = 'memcached' # session類型為memcached
app.config['SESSION_PERMANENT'] = True # 如果設(shè)置為T(mén)rue语淘,則關(guān)閉瀏覽器session就失效诲宇。
app.config['SESSION_USE_SIGNER'] = False # 是否對(duì)發(fā)送到瀏覽器上session的cookie值進(jìn)行加密
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 -
from flask import Flask, session
from flask_session import Session
app = Flask(__name__)
app.debug = True
app.secret_key = 'xxxx'
app.config['SESSION_TYPE'] = 'filesystem' # session類型為filesystem
app.config[
'SESSION_FILE_DIR'] = '/Users/wupeiqi/PycharmProjects/grocery/96.Flask新課程/組件/2.flask-session' # 文件路徑
app.config['SESSION_FILE_THRESHOLD'] = 500 # 存儲(chǔ)session的個(gè)數(shù)如果大于這個(gè)值時(shí),就要開(kāi)始進(jìn)行刪除了
app.config['SESSION_FILE_MODE'] = 384 # 文件權(quán)限類型
app.config['SESSION_PERMANENT'] = True # 如果設(shè)置為T(mén)rue惶翻,則關(guān)閉瀏覽器session就失效姑蓝。
app.config['SESSION_USE_SIGNER'] = False # 是否對(duì)發(fā)送到瀏覽器上session的cookie值進(jìn)行加密
app.config['SESSION_KEY_PREFIX'] = 'session:' # 保存到session中的值的前綴
Session(app)
@app.route('/index')
def index():
session['k1'] = 'v1
return 'xx'
if __name__ == '__main__':
app.run()
mongodb
#!/usr/bin/env python
# -*- coding:utf-8 -
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'] = 'mongo的db名稱(數(shù)據(jù)庫(kù)名稱)'
app.config['SESSION_MONGODB_COLLECT'] = 'mongo的collect名稱(表名稱)'
app.config['SESSION_PERMANENT'] = True # 如果設(shè)置為T(mén)rue,則關(guān)閉瀏覽器session就失效吕粗。
app.config['SESSION_USE_SIGNER'] = False # 是否對(duì)發(fā)送到瀏覽器上session的cookie值進(jìn)行加密
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操作簡(jiǎn)單示例:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from pymongo import MongoClient
# 創(chuàng)建鏈接
conn = MongoClient('47.93.4.198', 27017)
# 選擇數(shù)據(jù)庫(kù)
db = conn['db1']
# 選擇表
posts = db['posts']
post_data = {
'name': 'alex',
'age': 18
}
# 表中插入數(shù)據(jù)
# result = posts.insert_one(post_data)
# 獲取一條數(shù)據(jù)
# row = posts.find_one()
# print(row)
# # 獲取多條數(shù)據(jù)
# rows = posts.find()
# for row in rows:
# print(row)
# 刪除多條數(shù)據(jù)
# rows = posts.delete_many(filter={})
# print(rows)
# 更新多條數(shù)據(jù)
# posts.update({}, {'name': 'wupeiqi'})
sqlalchemy
#!/usr/bin/env python
# -*- coding:utf-8 -
import redis
from flask import Flask, session
from flask_session import Session as FSession
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.debug = True
app.secret_key = 'xxxx'
# 設(shè)置數(shù)據(jù)庫(kù)鏈接
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123@127.0.0.1:3306/fssa?charset=utf8'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
# 實(shí)例化SQLAlchemy
db = SQLAlchemy(app)
app.config['SESSION_TYPE'] = 'sqlalchemy' # session類型為sqlalchemy
app.config['SESSION_SQLALCHEMY'] = db # SQLAlchemy對(duì)象
app.config['SESSION_SQLALCHEMY_TABLE'] = 'session' # session要保存的表名稱
app.config['SESSION_PERMANENT'] = True # 如果設(shè)置為T(mén)rue纺荧,則關(guān)閉瀏覽器session就失效。
app.config['SESSION_USE_SIGNER'] = False # 是否對(duì)發(fā)送到瀏覽器上session的cookie值進(jìn)行加密
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()
PS: 在寫(xiě)好代碼后颅筋,不要著急運(yùn)行宙暇,需要先執(zhí)行進(jìn)入終端執(zhí)行一條創(chuàng)建數(shù)據(jù)庫(kù)表的命令:
bogon:pro-flask wupeiqi$ python3
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import db
>>> db.create_all()
>>>