简体   繁体   English

如何在Flask中存储用户的会话数据?

[英]How to store User's Data for Session in Flask?

I'm creating a Flask application that requires each request to use some data that a user has passed to the application to initialize their session. 我正在创建一个Flask应用程序,它要求每个请求使用用户已传递给应用程序的一些数据来初始化其会话。 Once they have passed that data and initialized the session, I'd like to re-use that data over and over until their session is done to fill requests without them having to pass the data for each request. 一旦他们传递了这些数据并初始化了会话,我就想一遍又一遍地重复使用这些数据,直到他们的会话完成填充请求,而不必为每个请求传递数据。

I can't seem to figure out the best way to do this. 我似乎无法找到最好的方法来做到这一点。 Storing it in the session variable doesn't work because that data just gets sent back to the user and then its the same as just passing it every request. 将它存储在会话变量中不起作用,因为该数据只是被发送回用户,然后就像每个请求传递它一样。 Storing the data in a database doesn't seem like the right choice because I need to throw it away at the end of the session, and I don't see any decorator to implement for when a session expires...so I'm afraid my database will just end up filling up with all of these data that come from initialization and no guaranteed way to remove them at the end of a session. 将数据存储在数据库中似乎不是正确的选择,因为我需要在会话结束时将其丢弃,并且我没有看到任何装饰器在会话到期时实现...所以我是害怕我的数据库最终会填满所有这些来自初始化的数据,并且无法保证在会话结束时删除它们。

Any suggestions? 有什么建议?

You have two options here: a session stored on the client, or a session stored on the server. 这里有两个选项:存储在客户端上的会话,或存储在服务器上的会话。

To store it on the server, you need a data-store. 要将其存储在服务器上,您需要一个数据存储。 If your app ever has to scale to any extent (including multiple uwsgi workers), you will have to use a distributed store. 如果您的应用程序必须扩展到任何程度(包括多个uwsgi工作者),您将不得不使用分布式存储。 If you have redis, that would be the best choice. 如果你有redis,那将是最好的选择。 See the Flask-Session example posted by Simon Fraser in the comments to do this. 请参阅Simon Fraser在评论中发布的Flask-Session示例。 It will handle storing the session object in your database and fetching values from it when needed. 它将处理在数据库中存储会话对象并在需要时从中获取值。 It handles a large number of backends as well, so whatever database you have will probably work with it out of the box. 它也可以处理大量的后端,因此无论您拥有什么数据库,都可以使用它开箱即用。

If you don't want to use a backend session, you have to use a client session. 如果您不想使用后端会话,则必须使用客户端会话。 This is done by setting a cookie- cookies are automatically attached to most requests the browser sends to your site, so values that you store on the cookie will usually come back to you. 这是通过设置cookie来完成的 - cookie会自动附加到浏览器发送到您网站的大多数请求中,因此您存储在cookie上的值通常会返回给您。 (this is how facebook remembers who you are without you logging in on every page. In fact, it is how the Flask-Session keeps track of WHICH session to restore!). (这就是facebook如果没有你登录每一页就记得你是谁。实际上,这就是Flask-Session如何跟踪要恢复的WHICH会话!)。 In order for this to be reliable, you have to sign the cookie so the user can't modify the values- Flask can handle this for you with the built in Session , or you can use a better crypto library as described Here . 为了使这是可靠的,你必须签署的cookie,因此用户不能修改值-瓶可以用内置的帮助您处理该会话 ,或者您也可以作为描述使用更好的加密库在这里 If you don't want the user to be able to see the values, or if you have a lot of data to store (the maximum size of a cookie is limited), you will have to use the server side version of all of this. 如果您不希望用户能够看到这些值,或者您要存储大量数据(cookie的最大大小有限),则必须使用所有这些的服务器端版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM