COOKIE 的使用
# coding:utf-8from flask import Flask, make_response, requestapp = Flask(__name__)@app.route("/set_COOKIE")
def set_COOKIE():resp = make_response("success")# 设置COOKIE, 默认有效期是临时COOKIE,浏览器关闭就失效resp.set_COOKIE("Flask", "Python")resp.set_COOKIE("Flask1", "Python1")# max_age设置有效期,单位:秒resp.set_COOKIE("Flask2", "Python1", max_age=3600)
# 在header 里面设置resp.headers["Set-COOKIE"] = "Flask3=Python3; Expires=Sat, 18-Nov-2017 04:36:04 GMT; Max-Age=3600; Path=/"return resp@app.route("/get_COOKIE")
def get_COOKIE():c = request.COOKIEs.get("Flask")return c@app.route("/delete_COOKIE")
def delete_COOKIE():resp = make_response("del success")# 删除COOKIEresp.delete_COOKIE("Flask1")return respif __name__ == '__main__':app.run(debug=True)
session 机制
session跨服务器
解决多台服务器之间共享session数据
设置session
# coding:utf-8from flask import Flask, sessionapp = Flask(__name__)# flask的session需要用到的秘钥字符串(用于加密)
app.config["SECRET_KEY"] = "dhsodfhisfhosdhf29fy989"# flask默认把session保存到了COOKIE中
@app.route("/login")
def login():# 设置session数据session["name"] = "Flask"session["mobile"] = "18611111111"return "login success"@app.route("/index")
def index():# 获取session数据name = session.get("name")return "hello %s" % nameif __name__ == '__main__':app.run(debug=True)
后面可以设置,将session 保存到redis服务器中