Python Flask,cookie,設定、獲取、刪除cookie
阿新 • • 發佈:2019-01-01
demo.py(設定、獲取、刪除cookie):
# coding:utf-8 from flask import Flask, make_response, request app = Flask(__name__) # 設定cookie @app.route("/set_cookie") def set_cookie(): resp = make_response("success") # "success"是響應體 # 設定cookie, 預設有效期是臨時cookie,瀏覽器關閉就失效 resp.set_cookie("Name", "Python") # max_age設定有效期,單位:秒 resp.set_cookie("Name2", "Python1", max_age=3600) # 設定cookie其實就是通過設定響應頭實現的。 # resp.headers["Set-Cookie"] = "Name3=Python3; Expires=Sat, 18-Nov-2017 04:36:04 GMT; Max-Age=3600; Path=/" return resp # 獲取cookie @app.route("/get_cookie") def get_cookie(): c = request.cookies.get("Name") return c # 刪除cookie @app.route("/delete_cookie") def delete_cookie(): resp = make_response("del success") # 刪除cookie resp.delete_cookie("Name1") return resp if __name__ == '__main__': app.run(debug=True)