当前位置:   article > 正文

Flask入门篇(五)cookie和session操作_flask-session-cookie-manager-master

flask-session-cookie-manager-master
from flask import Flask, session


app = Flask(__name__)


"""cookie操作"""
# 设置cookie
@app.route("/login_success")
def login_success():
    response = make_response("login success")
    response.set_cookie("t", "aaaaa")
    response.set_cookie("n", "12345", max_age=3600)
    return response
# 获取cookie
@app.route("/get_cookie")
def get_cookie():
    t = request.cookies.get("t")
    n = request.cookies.get("n")
    return t + n
# 删除cookie
@app.route("/delete_cookie")
def delete_cookie():
    response = make_response("delete success")
    response.delete_cookie("t")
    return response


"""
在flask中要使用session需要用到秘钥字符串,
解析1:在flask中session默认是保存在前端的,利用签名的方法对session进行混淆加密
解析2:混淆加密的规则就是利用app.config["SECRET_KEY"]和session值就行混淆
解析3:即使被破解出来了,flask有反篡改机制,一旦被篡改flask就认为session无效了
"""
app.config["SECRET_KEY"] = "asd44sSADdsaasdlj87"


@app.route("/login")
def login():
    session["uid"] = "zhangsan"
    session["pwd"] = "123456"
    return "login success"


@app.route("/index")
def index():
    uid = session.get("uid")
    return "welcome %s" % uid


if __name__ == '__main__':
    app.run(debug=True)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/540571
推荐阅读
相关标签
  

闽ICP备14008679号