如何仅通过SID获得当前会话? [英] How to get current session with only the SID?

查看:124
本文介绍了如何仅通过SID获得当前会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅使用其会话标识符(SID)检索会话?我使用 gae-sessions



更新回应@David Underhill:

我已经完成了你的建议,但是在调试模式下,我得到了:
Session:SID = None {} | 但它有 db_key 填充字符串。



以下是我的代码:

upload.py

 SID = self.request.get(SID)
如果不是SID:
返回False

from app.models.user import User
user = User.get_current_user(SID)

user.py < (SID =无):
如果SID:
会话=会话(sid)


  def get_current_user = SID)
else:
session = get_current_session()

如果session.has_key('account.logged'):
返回User.get(db.Key (session.get('user.key')))
else:
返回False

并不存在。



这是




mMumLZOCq5knTg9edaSjLD + p8xsSohAQSS9vyqpgDso = 1289155992_9c2c85c6931a7d133f7e1f0b9ae25502gAJ9cQB9cQEoVQ5hY2NvdW50LmxvZ2dlZHECiFUIdXNlci5rZXlxA1UjYWd0MGIzUjBlWE4zYjNKc1pISUxDeElFVlhObGNoaTNBUXdxBFULYWNjb3VudC5rZXlxBVUnYWd0MGIzUjBlWE4zYjNKc1pISU9DeElIUVdOamIzVnVkQmkyQVF3cQZVDWFjY291bnQuZW1haWxxB1UBdHEIdYZxCS4 =


$ b:从我的cookie我的SID
$ b

我从Cookie中获取这些信息,包括JavaScript和Flex,然后将其发送给一个变量给Python。

解决方案

要检索仅使用SID的会话,请直接实例化 Session 类:

  session = gaesessions.Session(sid =你想要的会话的SID)



<如果您只想使用会话ID检索会话,那么该会话必须已被持久化到数据存储或memcache。默认情况下, gae-sessions 仅将小会话存储在安全Cookie中,这些会话比memcache或数据存储。



要强制将特定会话保存到数据存储,您可以调用 save(persit_even_if_using_cookie = True)。这将强制将其存储到数据存储/ memcache,即使它通常只存储在cookie中。如果您仅需要偶尔通过SID访问用户会话,则这是最好的方法。



如果您需要经常通过SID访问会话,那么禁用可能会更好当您配置它时,通过将 cookie_only_threshold = 0 传递给 SessionMiddleware - 这将确保cookie是唯一的cookie机制从未使用,并且您的会话始终在数据存储区中。



这些详细信息和更多信息在 gae-sessions文档


How can I retrieve a session using only its session identifier (SID)? I'm using gae-sessions.

Update for response @David Underhill:

I've done what you suggested, but in debug mode I got this: Session: SID=None {} | but it has db_key populated with a string.

Here is my code:

upload.py

SID = self.request.get("SID")
if not SID:
return False

from app.models.user import User
user = User.get_current_user(SID)

user.py

def get_current_user(SID = None):
    if SID:
        session = Session(sid=SID)
    else:
        session = get_current_session()

    if session.has_key('account.logged'):
        return User.get(db.Key(session.get('user.key')))
    else:
        return False

and doesn't exists.

And this is my SID from my cookie:

"mMumLZOCq5knTg9edaSjLD+p8xsSohAQSS9vyqpgDso=1289155992_9c2c85c6931a7d133f7e1f0b9ae25502gAJ9cQB9cQEoVQ5hY2NvdW50LmxvZ2dlZHECiFUIdXNlci5rZXlxA1UjYWd0MGIzUjBlWE4zYjNKc1pISUxDeElFVlhObGNoaTNBUXdxBFULYWNjb3VudC5rZXlxBVUnYWd0MGIzUjBlWE4zYjNKc1pISU9DeElIUVdOamIzVnVkQmkyQVF3cQZVDWFjY291bnQuZW1haWxxB1UBdHEIdYZxCS4="

I take this from cookies, with JavaScript and Flex, then send it in a variable to Python.

解决方案

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.

这篇关于如何仅通过SID获得当前会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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