Flask of third-party components Flask-Session

Native session: mechanism custody by the client, security is relatively poor, the advantage is that is not occupied server space

Flask-Session: solve the disadvantages native session of the installation package

from flask import Flask ,request,session
from flask_session import Session

def create_app():
    app = Flask(__name__)
    app.config["DEBUG"] = True
    # 写如下的配置就已经在redis中存了一个session ;但是也依然存在浏览器中
    #查看session 启动redis服务之后  redis-cli get "session:浏览器复制key"
    app.config["SESSION_TYPE"] = "redis"
    # 设置redis客户端
    app.config["SESSION_REDIS"] = "redis"

    # 在Config之后;在蓝图导入之前
    Session(app)

    app.register_blueprint(user)

    return app

# 视图函数中添加路由
@user.route("/login")
def login():
    # ed61d7f3-df98-4d98-9966-a7f22f40a2de
    session["user"] = "anwen"
    return "user 登录成功"

Guess you like

Origin www.cnblogs.com/an-wen/p/11601366.html