标签:move val data time add 原理 ace get adsi
@app.route("/", )
def index():
# 如何设置sessoion
# 1 导入session
# 2 给sessoion设置值
# 设置 session
session['name'] = "santa"
session["nam1"] = "kai"
return "ok"
@app.route("/login/")
def login():
# 获取 session
# name = session.get('name')
print(session["name"]) # santa
return "login"
if __name__ == '__main__':
app.run()
分析 session 的原理
class SecureCookieSessionInterface(SessionInterface):
salt = "cookie-session"
digest_method = staticmethod(hashlib.sha1)
key_derivation = "hmac"
serializer = session_json_serializer
session_class = SecureCookieSession
def get_signing_serializer(self, app):
if not app.secret_key:
return None
signer_kwargs = dict(
key_derivation=self.key_derivation, digest_method=self.digest_method
)
return URLSafeTimedSerializer(
app.secret_key,
salt=self.salt,
serializer=self.serializer,
signer_kwargs=signer_kwargs,
)
# 取session的时候执行的
def open_session(self, app, request):
s = self.get_signing_serializer(app)
if s is None:
return None
##cookie键是SESSION_COOKIE_NAME"=session
val = request.cookies.get(app.session_cookie_name)
print("open_session.session_cookie_name,", app.session_cookie_name, )
if not val:
return self.session_class()
max_age = total_seconds(app.permanent_session_lifetime)
try:
data = s.loads(val, max_age=max_age)
print("self.session_class(data)", self.session_class(data) )
return self.session_class(data)
except BadSignature:
return self.session_class()
#存session的时候执行的
def save_session(self, app, session, response):
domain = self.get_cookie_domain(app)
path = self.get_cookie_path(app)
# If the session is modified to be empty, remove the cookie.
# If the session is empty, return without setting the cookie.
if not session:
if session.modified:
response.delete_cookie(
app.session_cookie_name, domain=domain, path=path
)
return
# Add a "Vary: Cookie" header if the session was accessed at all.
if session.accessed:
response.vary.add("Cookie")
if not self.should_set_cookie(app, session):
return
httponly = self.get_cookie_httponly(app)
secure = self.get_cookie_secure(app)
samesite = self.get_cookie_samesite(app)
expires = self.get_expiration_time(app, session)
# 把session做了一个加密,把整个session的key--》val,全部加密,的到一个value值,
#session是一个大字典,
val = self.get_signing_serializer(app).dumps(dict(session))
# 他把session加密后得到的val存到cookie里面了
#cookie键是SESSION_COOKIE_NAME"=session
print("源码中的session",dict(session))
print("app.session_cookie_name,",app.session_cookie_name,)
response.set_cookie(
app.session_cookie_name,
val,
expires=expires,
httponly=httponly,
domain=domain,
path=path,
secure=secure,
samesite=samesite,
)
标签:move val data time add 原理 ace get adsi
原文地址:https://www.cnblogs.com/kai-/p/12521132.html