[mod_python] Lost Session

Jim Gallacher jg.lists at sympatico.ca
Mon Jun 20 12:09:11 EDT 2005

Jeremy Kohansimeh wrote:
> Hello,
> I am having trouble recovering a session.  On the initial request, I 
> create some entries for the session and then save it (I have confirmed 
> that there is data in the session before I save it and that the save() 
> function is being called).  On subsequent requests, the session object 
> that I have created is not reinstated, instead a new one is created (I 
> have coonfirmed this with the is_new() function).  I have checked my 
> browser and there is a pysid cookie being set on the first request.

Is pysid the same for subsequent requests? I think something got out of 

> Does anyone have any tips to debug this?  I am really at a loss for what 
> is going on.
> Here is the code that I use to reinstate/create a session:
> def handler(req, **kw):
>     """handles initial requests"""
>     f = open( LOGFILE, 'a+')
>     req.sess = Session.Session( req)

Session.Session() loads the session if it exists, otherwise creates a 
new one, as well as a new session cookie / session id. The new session 
*does not* automatically get saved. You need to call req.sess.save() at 
some point. I know you know this. ;)

>     if not req.sess.is_new():
>         # this is an old session, so load it up
>         req.sess.load()

req.sess.load() is redundant.

>         f.write( 'old session\n')
>     else:
>         f.write( 'new session\n')

It's a new session (for whatever reason) with a new pysid in the cookie 
sent to the client, but we never call req.sess.save() in this request. 
Henceforth, every request will generate a new session.


More information about the Mod_python mailing list