赞
踩
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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。