Flask会话会忘记请求之间的条目 [英] Flask session forgets entry between requests

查看:213
本文介绍了Flask会话会忘记请求之间的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用最新的Flask / Werkzeug(Flask 0.9)客户端会话来在请求之间保持信息。会话没有被设置为持久性的(因为我可以在浏览器关闭时删除cookie)。



我的问题如下:

>

我使用一些服务器端代码来填充Flask会话变量。在这之后,Session变量看起来像这样:

 < SecureCookieSession {u'items':SOMENOTVERYIMPORTANTDICTIONARY},'_fresh' :True,'user_id':u'1','csrf':'0aef1995cdf2cxx0233fdf3321d17fc7267f3b32','_id':'someUNIQUEcode'} *> 

我使用这个信息来渲染一个执行GET请求(通过JQuery)到同一个Flask应用程序,但突然间会话中包含'items'条目的字典不见了:

 < SecureCookieSession {'_fresh': True,'user_id':u'1','csrf':'0aef1995cdf2cxx0233fdf3321d17fc7267f3b32','_id':'someUNIQUEcode'}> 

我做了一些四处搜寻,并认为这可能与我正在测试在本地主机上(1​​27.0.0.1和localhost不一样)。我修复了我的主机文件并添加了一个dev.localhost条目,以确保所有请求都来自同一个主机。



另外,我的浏览器Chrome浏览器)显示完全相同的标识符,以发送到服务器的会话cookie。



另外,设置 session.modified = True



   

为第一个请求(填充 items entry)与第二个请求的比较

  __ utmb = 122666782.19.10.1363877633 

认为它仍然可能是一个Ajax相关的东西。我在一个简单的页面重载之后测试了会话变量的内容: items 条目仍然从会话中消失。



<如果您发现客户端(Chrome)返回的Cookie超过了您的帐户,那么您将无法获得任何帮助。 cookie大小的4096字节限制。显然,Django默认使用服务器端会话,这使得这个问题只有在我将代码移动到Flask时才会出现。在Flask中使用服务器端会话(如 flask-kvsession 等)应该解决这个问题。


I'm using the latest Flask/Werkzeug (Flask 0.9) client-side sessions to persist information between requests. The session is not set to be persistent (as I'm fine with the cookie being deleted when the browser is closed).

My problem is as follows:

I use some server-side code to fill the Flask session variable with an entry. After this, the Session variable looks something like this:

<SecureCookieSession {u'items': SOMENOTVERYIMPORTANTDICTIONARY}, '_fresh': True, 'user_id': u'1', 'csrf': '0aef1995cdf2cxx0233fdf3321d17fc7267f3b32', '_id': 'someUNIQUEcode'}*>

I use this information to render a page that performs a GET request (through JQuery) to the same Flask application, but suddenly the dictionary containing the 'items' entry in the session is gone:

<SecureCookieSession {'_fresh': True, 'user_id': u'1', 'csrf': '0aef1995cdf2cxx0233fdf3321d17fc7267f3b32', '_id': 'someUNIQUEcode'}>

I did some searching around, and thought that it may be related to the fact that I'm testing on localhost (127.0.0.1 is not the same as localhost). I fixed my hosts file and added a 'dev.localhost' entry to make sure that all requests are from the same host.

Also, the developer pane of my browser (Chrome) shows exactly the same identifiers for the session cookies being sent to the server.

Also, setting session.modified = True does not help.

The only thing that changes between requests is

__utmb=122666782.18.10.1363877633

for the first request (the one that populates the items entry) vs. the second request

__utmb=122666782.19.10.1363877633

Thinking that it still may be an Ajax-related-thing. I tested the contents of the session variable after a straightforward page reload: the items entry is still gone from the session.

Any help would be greatly appreciated.

解决方案

It turns out that the cookie being sent back to the client (Chrome) exceeds the 4096 bytes limit for cookie size. Apparently Django uses server-side sessions by default, which made this problem only appear when I moved my code to Flask. Using server side sessions in Flask such as in flask-kvsession and others should fix the issue.

这篇关于Flask会话会忘记请求之间的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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