Home > Article > Backend Development > Introduction to the method of session setting of flask in python
This article brings you an introduction to the session setting method of flask in python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
What is Session?
Cookie: client browser cache;
session: server server cache;
Session is similar to Cookie, both pass Dictionary manages key-value pairs.
Session object stores the properties and configuration information required for a specific user session. In this way, when the user jumps between Web pages in the application, the variables stored in the Session object will not be lost, but will persist throughout the user session. When a user requests a Web page from an application, the Web server automatically creates a Session object if the user does not already have a session. When a session expires or is abandoned, the server terminates the session. One of the most common uses of Session objects is to store user preferences.
Reading and writing Session can be done by operating the dictionary.
import random from flask import Flask, session app = Flask(__name__) # 因为flask的session是通过加密之后放到了cookie中。所以有加密就有密钥用于解密,所以, # 只要用到了flask的session模块就一定要配置“SECRET_KEY”这个全局宏。一般设置为24位的字符 app.config['SECRET_KEY'] = random._urandom(24) # 设置session值; @app.route('/') def index(): # 如何设置session的key-value值 session['name']='sheen' return 'hello,sheen' @app.route('/get/') def get(): # 获取Session的key-vlaue值 print(len(session)) return session.get('name') @app.route('/delete/') def delete(): # 删除Session的key-vlaue值 session.pop('name') print(session.get('name')) return 'session was deleted' app.run()
##
The above is the detailed content of Introduction to the method of session setting of flask in python. For more information, please follow other related articles on the PHP Chinese website!