In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to use session in Flask. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Flask-session usage
Brief introduction
Flask-session is the session component of the flask framework. Since the original flask built-in session is saved using a signature cookie, this component will support session to be saved in multiple places, such as:
Redis: a tool for saving data, five types. Non-relational database
Memcached
Filesystem
Mongodb
Sqlalchmey: that data is stored in the database table
Installation
Pip3 install flask-session
Video explanation
Storage mode
Redis
#! / usr/bin/env python#-*-coding:utf-8-import redisfrom flask import Flask, sessionfrom flask_session import Sessionapp = Flask (_ _ name__) app.debug = Trueapp.secret_key = 'xxxx'app.config [' SESSION_TYPE'] = 'redis' # session type is redisapp.config [' SESSION_PERMANENT'] = False # if set to True, then closing the browser session will have no effect. App.config ['SESSION_USE_SIGNER'] = False # whether to encrypt cookie values sent to session on browsers app.config [' SESSION_REDIS'] = 'session:' # prefix app.config [' SESSION_REDIS'] = redis.Redis (host='127.0.0.1', port='6379') of values saved to session Password='123123') # configuration for connecting to 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 memcacheapp = Flask (_ _ name__) app.debug = Trueapp.secret_key = 'xxxx'app.config [' SESSION_TYPE'] = 'memcached' # session type is redisapp.config [' SESSION_PERMANENT'] = True # if set to True, closing the browser session will have no effect. App.config ['SESSION_USE_SIGNER'] = False # whether to encrypt cookie values sent to session on the browser app.config [' SESSION_KEY_PREFIX'] = 'session:' # prefix app.config of values saved to session [' SESSION_MEMCACHED'] = memcache.Client (['10.211.55.4session 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 Sessionapp = Flask (_ _ name__) app.debug = Trueapp.secret_key = 'xxxx'app.config [' SESSION_TYPE'] = 'filesystem' # session type is redisapp.config [' SESSION_FILE_DIR'] ='/ Users/wupeiqi/PycharmProjects/grocery/96.Flask new curriculum / component / 2.flaskMusessionsession' # session type is redisapp.config ['SESSION_FILE_THRESHOLD'] = 500 # the number of storage session is greater than this value App.config ['SESSION_FILE_MODE'] = 384 # File permission type app.config [' SESSION_PERMANENT'] = True # if it is set to True, then closing the browser session is invalid. App.config ['SESSION_USE_SIGNER'] = False # whether to encrypt cookie values sent to session on the browser app.config [' SESSION_KEY_PREFIX'] = 'session:' # prefix Session (app) @ app.route (' / index') def index (): session ['K1'] =' v1' session ['K2'] =' v1' return 'xx'if _ name__ = ='_ _ main__': app.run ()
Mongodb video explanation
#! / 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 type is redisapp.config [' SESSION_MONGODB'] = pymongo.MongoClient () app.config ['SESSION_MONGODB_DB'] =' db name of mongo (database name) 'app.config [' SESSION_MONGODB_COLLECT'] = 'collect name of mongo (table name)' app.config ['SESSION_PERMANENT'] = True # if set to True If you close the browser, session will become invalid. App.config ['SESSION_USE_SIGNER'] = False # whether to encrypt cookie values sent to session on the browser app.config [' SESSION_KEY_PREFIX'] = 'session:' # prefix Session (app) @ app.route (' / index') def index (): session ['K1'] =' v1' session ['K2'] =' v1' return 'xx'if _ name__ = ='_ _ main__': app.run ()
Simple example of mongodb operation:
#! / usr/bin/env python#-*-coding:utf-8-*-from pymongo import MongoClient# create link conn = MongoClient ('47.93.4.198, 27017) # Select database db = conn [' db1'] # Select table posts = db ['posts'] post_data = {' name': 'alex' 'age': 18} # insert data # result = posts.insert_one (post_data) # get a piece of data # row = posts.find_one () # print (row) # # get multiple pieces of data # rows = posts.find () # for row in rows:# print (row) # Delete multiple pieces of data # rows = posts.delete_many (filter= {}) # print (rows) # Update multiple pieces of data # posts.update ({}) {'name':' wupeiqi'})
Sqlalchemy
#! / usr/bin/env python#-*-coding:utf-8-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 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# instantiation SQLAlchemydb = SQLAlchemy (app) app.config ['SESSION_TYPE'] =' sqlalchemy' # session type is sqlalchemyapp.config ['SESSION_SQLALCHEMY'] = db # SQLAlchemy object app.config [' SESSION_SQLALCHEMY_TABLE'] = 'session' # session the name of the table to be saved is app.config [' SESSION_PERMANENT'] = True # if set to True If you close the browser, session will become invalid. App.config ['SESSION_USE_SIGNER'] = False # whether to encrypt cookie values sent to session on the browser app.config [' SESSION_KEY_PREFIX'] = 'session:' # prefix FSession (app) @ app.route (' / index') def index (): session ['K1'] =' v1' session ['K2'] =' v1' return 'xx'if _ name__ = ='_ _ main__': app.run ()
PS: after writing the code, don't rush to run it. You need to enter the terminal and execute a command to create a database table:
Bogon:pro-flask wupeiqi$ 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 () > >
Application scenario
If the application is small, save the session with the native encrypted ccokie (built-in)
If the application is large, use redis (flask-session)
The above is how to use session in Flask. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.