- 设置cookie,默认有效期是临时cookie,浏览器关闭就会失效
from flask import Flask,make_responseapp = Flask(__name__)@app.route("/set_cookie",methods=["GET"])
def set_cookie():resp = make_response("success")resp.set_cookie("Zep","python")resp.set_cookie("Zep1", "python1")return respif __name__ == '__main__':app.run(debug=True)
- 通过max_age设置有效期,单位:秒
- 读取cookie的值
- 删除cookie
- 设置cookie的实质:在响应头中添加数据
完整代码:
from flask import Flask,make_response,requestapp = Flask(__name__)@app.route("/set_cookie",methods=["GET"])
def set_cookie():resp = make_response("success")resp.set_cookie("Zep","python")resp.set_cookie("Zep1", "python1")resp.set_cookie("Zep2", "python2",max_age=3600)resp.headers["Set-Cookie"] = "Zep3=python3; Expires=Fri, 05-Jun-2020 13:51:38 GMT; Max-Age=3600; Path=/"return resp
@app.route("/get_cookie")
def get_cookie():c = request.cookies.get("Zep")return c
@app.route("/delete_cookie")
def delete_cookie():resp = make_response("del success")resp.delete_cookie("Zep")return respif __name__ == '__main__':app.run(debug=True)