Python + Flask - 从会话中删除密钥太快 [英] Python + Flask - Removing key from session too fast

查看:21
本文介绍了Python + Flask - 从会话中删除密钥太快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 API 服务可以做到这一点:

I have and API service that make this:

session[parent].pop(child)

但是,当我在浏览器中多次同时调用此服务时,这对于 Flask(或 Python,或者我不知道)来说似乎太快了.只删除了一个孩子".我尝试使用

But, when I call this service more than one time and at the same time in Browser, this seems too fast for Flask (or Python, or I don't know). Just one 'child' is removed. I tried to use

del session[parent][child]

但问题是一样的.我可以在我的 API 服务中获取一个值列表来解决这个问题,但是,我想了解为什么会发生这种情况.

but the problem was the same. I can get a list of values in my API Service to resolve that, but, I want to understand why this is happening.

我不知道是 Flask 问题、Python 问题还是Web Stuff"问题...

I don't know if it is a Flask problem, a Python problem, a 'Web Stuff' problem...

推荐答案

这是一个Web Stuff"问题.

It's a 'Web Stuff' problem.

会发生什么是浏览器存储它收到的最后一个版本.但是,如果它收到乱序的响应,或者您在请求完成之前中止请求,浏览器将不会存储该版本.

What happens is that the browser stores the last version it received. But if it receives responses out of order, or you abort a request before it is completed, the browser won't be storing that version.

Flask 将 session 的数据完全存储在 cookie 中.除了用于加密和解密内容的服务器端机密之外,服务器端没有存储任何内容.

Flask stores the data for session entirely in a cookie. There is nothing stored on the server side other than the server-side secret used to encrypt and decrypt the contents.

带有该 cookie 的响应会发送到浏览器,浏览器会存储该 cookie.这对浏览器来说是完全不透明的数据,因为它是经过压缩和加密签名的,所以它不能对它做任何事情.

A response with that cookie is sent to the browser, and the browser stores the cookie. This is an entirely opaque piece of data to the browser, it cannot do anything with it as it is compressed and cryptographically signed.

然后,浏览器会在每次发出请求时按原样将该 cookie 发送回 Flask 服务器.如果对该请求的响应包含 cookie 的新版本,那么它将存储在浏览器 cookie 存储中.任何在存储后启动的新请求都将使用新的 cookie.

The browser will then send that cookie as is back to the Flask server every time a request is made. If the response to that request contains a new version of the cookie, then that'll be stored in the browser cookie storage. Any new request started after storing will then use the new cookie.

但是,如果您在响应完全处理之前开始请求,或者没有完成响应处理,那么可以使用旧的 cookie 并且您的服务器使用更改解码旧版本没有制作.

If however, you start a request before a response has been fully processed, or did not complete handling the response, then an older cookie could be used and your server decodes that older version with the changes not made.

这篇关于Python + Flask - 从会话中删除密钥太快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆