2

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.

bruntime
  • 371
  • 2
  • 13
Totty.js
  • 15,563
  • 31
  • 103
  • 175

1 Answers1

2

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.

David Underhill
  • 15,896
  • 7
  • 53
  • 61
  • check out the update in my question. still doesn't work :s I can only try next the cookie_only_threshold=0 – Totty.js Oct 30 '10 at 22:30
  • After you do the `save(persit_even_if_using_cookie=True)` the session should appear in the datastore. Can you manually query the datastore to see if the session is there? (It should be the only one if you typically use cookie-only sessions). Good thought setting dirty=True (only required if you haven't changed anything in your session - since you set user.key, you don't need to set dirty). – David Underhill Oct 30 '10 at 23:25
  • yes, I check in the datastore viewer and exits. I really don't understand why I still can't get my session by sid.. :s – Totty.js Oct 31 '10 at 15:14
  • Strange; this works for me. Could you perhaps post a small code sample which illustrates the issue? (Or your whole project if you're okay with sharing and don't want to factor out a small sample) – David Underhill Oct 31 '10 at 16:36
  • Great, glad you were able to fix it. – David Underhill Nov 01 '10 at 00:30