How to get current session with only the SID?

倖福魔咒の 提交于 2019-12-06 11:44:26

To retrieve a session used only its SID, directly instantiate the Session class:

session = gaesessions.Session(sid="SID of the session you want")

If you want to retrieve a session using only its session ID, then that session must have been persisted to the datastore or memcache. By default, gae-sessions only stores small sessions in secure cookies which are much faster than either memcache or the datastore.

To force a particular session to be saved to the datastore, you can call save(persit_even_if_using_cookie=True) on the session object. This will force it to be stored to the datastore/memcache even if it normally would only be stored in a cookie. This is the best approach if you only need to occasionally access user sessions by SID alone.

If you need to frequently access sessions by SID, then you might be better off disabling the cookie-only mechanism by passing cookie_only_threshold=0 to the SessionMiddleware when you configure it - this will ensure that cookies are never used and that your sessions are always in the datastore.

These details and more are documented more thoroughly in the gae-sessions documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!