Flask-session component,
Introduction
Flask-session is the session component of the flask framework. Because the original flask built-in session is saved using the signature cookie, this component stores the session in multiple places, for example:
- Redis
- Memcached
- Filesystem
- Mongodb
- Sqlalchmey
Install
pip3 install flask-session
Storage Mode redis
1 import redis 2 from flask import Flask, session 3 from flask_session import Session 4 5 app = Flask (_ name _) 6 app. debug = True 7 app. secret_key = 'xxxx' 8 9 app. config ['session _ type'] = 'redis '# The session type is redis10 app. config ['session _ PERMANENT '] = False # if it is set to True, closing the browser SESSION becomes invalid. 11 app. config ['session _ USE_SIGNER '] = False # Whether to encrypt the cookie value sent to the SESSION on the browser 12 app. config ['session _ key_prefix'] = 'session: '# prefix 13 app for the value saved to the SESSION. config ['session _ REDIS '] = redis. redis (host = '2017. 0.0.1 ', port = '000000', password = '000000') # used to connect to redis configuration 14 15 Session (app) 16 17 18 @ app. route ('/Index') 19 def index (): 20 session ['k1 '] = 'v1 '21 return 'xx' 22 23 24 if _ name _ =' _ main _ ': 25 app. run ()
Store memcached in redis
Import redisfrom flask import Flask, sessionfrom flask_session import Sessionimport memcacheapp = Flask (_ name _) app. debug = Trueapp. secret_key = 'xxxx' app. config ['session _ type'] = 'memcached' # The session type is redisapp. config ['session _ PERMANENT '] = True # if it is set to True, closing the browser SESSION becomes invalid. App. config ['session _ USE_SIGNER '] = False # Whether to encrypt the cookie value sent to the SESSION on the browser. config ['session _ key_prefix'] = 'session: '# prefix of the value saved to the 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 ()
Memcached storage filesystem
Import redisfrom flask import Flask, sessionfrom flask_session import Sessionapp = Flask (_ name _) app. debug = Trueapp. secret_key = 'xxxx' app. config ['session _ type'] = 'filesystem' # The session type is redisapp. config ['session _ FILE_DIR '] ='/Users/wupeiqi/PycharmProjects/grocery/96. new Flask Course/component/2. flask-session '# The session type is redisapp. config ['session _ FILE_THRESHOLD '] = 500 # Delete the app if the number of stored sessions exceeds this value. con Fig ['session _ FILE_MODE '] = 384 # File Permission type app. config ['session _ PERMANENT'] = True # if it is set to True, closing the browser SESSION becomes invalid. App. config ['session _ USE_SIGNER '] = False # Whether to encrypt the cookie value sent to the SESSION on the browser. config ['session _ key_prefix'] = 'session: '# prefix of the value saved to the SESSION session (app) @ app. route ('/Index') def index (): session ['k1 '] = 'v1 'session ['k2'] = 'v1 'Return 'xx' if _ name _ =' _ main __': app. run ()
Filesystem stores mongodb
Import redisfrom flask import Flask, sessionfrom flask_session import Sessionimport py1_app = Flask (_ name _) app. debug = Trueapp. secret_key = 'xxxx' app. config ['session _ type'] = 'mongodb '# The session type is redisapp. config ['session _ MONGODB '] = pymongo. consumer Client () app. config ['session _ MONGODB_DB '] = 'database name of mongo (Database Name)' app. config ['session _ MONGODB_COLLECT '] = 'Collect name of mongo (Table Name)' app. config ['session _ PERMA NENT '] = True # if it is set to True, closing the browser session becomes invalid. App. config ['session _ USE_SIGNER '] = False # Whether to encrypt the cookie value sent to the SESSION on the browser. config ['session _ key_prefix'] = 'session: '# prefix of the value saved to the SESSION session (app) @ app. route ('/Index') def index (): session ['k1 '] = 'v1 'session ['k2'] = 'v1 'Return 'xx' if _ name _ =' _ main __': app. run ()
Simple mongodb storage operation example:
From pymongo import Consumer Client # create link conn = Consumer Client ('47. 93.4.198 ', 27017) # select database db = conn ['db1'] # select table posts = db ['posts'] post_data = {'name': 'xxx ', 'age': 18} # insert data in the table # result = posts. insert_one (post_data) # Get a piece of data # row = posts. find_one () # print (row) # obtain multiple data records # rows = posts. find () # for row in rows: # print (row) # Delete multiple data entries # rows = posts. delete_rows (filter ={}) # print (rows) # update multiple data records # posts. update ({},{ 'name': 'xxx '})
Instance sqlalchemy
Import redisfrom flask import Flask, sessionfrom flask_session import Session as FSessionfrom flask_sqlalchemy import SQLAlchemyapp = Flask (_ name _) app. debug = Trueapp. secret_key = 'xxxx' # Set the database link app. config ['sqlalchemy _ DATABASE_URI '] = 'mysql + pymysql: // root: 123@127.0.0.1: 3306/fssa? Charset = utf8' app. config ['sqlalchemy _ TRACK_MODIFICATIONS '] = True # instantiate SQLAlchemydb = SQLALCHEMY (app) app. config ['session _ type'] = 'sqlalchemy '# The session type is sqlalchemyapp. config ['session _ SQLALCHEMY '] = db # SQLAlchemy object app. config ['session _ SQLALCHEMY_TABLE '] = 'session' # Name of the table to be saved in the SESSION app. config ['session _ PERMANENT '] = True # if it is set to True, closing the browser SESSION becomes invalid. App. config ['session _ USE_SIGNER '] = False # Whether to encrypt the cookie value sent to the SESSION on the browser. config ['session _ key_prefix'] = 'session: '# prefix of the value saved to the SESSION FSession (app) @ app. route ('/Index') def index (): session ['k1 '] = 'v1 'session ['k2'] = 'v1 'Return 'xx' if _ name _ =' _ main __': app. run ()
Sqlalchemy Storage
PS: After writing the code, do not worry about running it. You need to run the following command on the terminal to create a database table:
bogon:pro-flask $ python3Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> from app import db>>> db.create_all()>>>
View Code