Home > Backend Development > Python Tutorial > Introduction to the method of session setting of flask in python

Introduction to the method of session setting of flask in python

不言
Release: 2018-10-25 15:59:06
forward
2816 people have browsed it

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()
Copy after login

Introduction to the method of session setting of flask in python

Introduction to the method of session setting of flask in python

Introduction to the method of session setting of flask in python

Introduction to the method of session setting of flask in python ##

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template