python中的session是什么

python中的session是什么,第1张

Session:在计算机中,尤其是在网络应用中,称为“会话控制”。Session对象存储特定用户会话所需的属性及配置信息。这样,当用户

在应用程序的Web页之间跳转时,存储在Session对象中的变量将不会丢失,而是在整个用户会话中一直存在下去。当用户请求来自应用

程序的 Web页时,如果该用户还没有会话,则Web服务器将自动创建一个 Session对象。当会话过期或被放弃后,服务器将终止该会

话。Session 对象最常见的一个用法就是存储用户的首选项。例如,如果用户指明不喜欢查看图形,就可以将该信息存储在Session对象

中。注意会话状态仅在支持cookie的浏览器中保留。

推荐学习《python教程》。

官网有写,不过看的不是很懂。

大致意思是Session用于创建程序与数据库之间的会话,并将这个会话以python的面向对象方式(“holding zone”)进行包装,提供给python其他程序调用。

Session提供一个进行数据库查询(query)的入口,python程序通过Session实例,可以对正在连接的数据库进行查询(查询结果是一个面向对象实例),将修改的内容(一般是修改面向对象实例,然后通过Session将修改的实例与数据库中特定数据“同步”。)

Session中有一个很重要的理念“Identity Map”。Session查询的结果实例,我们修改过的实例,和数据库中的数据是怎么关联起来的呢?就是靠“Identity Map”,每个实例都具有“Identity Map”记录,每个“Identity Map”都和数据行的主键(primary key)一一对应。

(这是我的渣英语翻译+个人理解写的。有不对的地方,欢迎大神指点)

官网内容:

In the most general sense, the Session establishes all conversations with the database and represents a “holding zone” for all the objects which you’ve loaded or associated with it during its lifespan. It provides the entrypoint to acquire a Query object, which sends queries to the database using the Session object’s current database connection, populating result rows into objects that are then stored in the Session, inside a structure called the Identity Map - a data structure that maintains unique copies of each object, where “unique” means “only one object with a particular primary key”.

官网资料:http://docs.sqlalchemy.org/en/rel_0_9/orm/session_basics.html#what-does-the-session-do

session.get('known',

False)

意思就是说session这个对象里面有known

这个属性就返回这个属性的值

session['known']

=

False

else:

session['known']

=

True

比如你这里设置了它的值为False

或者True,那么它就返回True

或者False

如果你没有设置它的值

就返回你写的

get('known',

False)默认值False


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9257086.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-26
下一篇2023-04-26

发表评论

登录后才能评论

评论列表(0条)

    保存