[mod_python] Getting an Old Session

Jim Gallacher jg.lists at sympatico.ca
Wed Jul 13 19:59:41 EDT 2005

Jeremy Kohansimeh wrote:
> Hello,
> I have a problem where I am not able to reinstate an old Session.  On 
> initial login I record the session id and then check that the pysid 
> cookie has been set appropriately in my browser, which it has.  But, on 
> a subsequent call when I try to reinstate the same session, I get a new 
> one back that no longer has any of the items I have saved.  I have been 
> using the following code for diagnostics:
> def handler(req, **kw):
>     """handles initial requests"""
>     # used for debugging
>     f = open( '/home/jeremyk/public_html/data/foo.log', 'a+')
>     cookies = Cookie.get_cookies( req)
>     if cookies.has_key( 'pysid'):
>         pysid = cookies[ 'pysid'].value
>         f.write( 'pysid is ' + pysid + '\n')
>         req.sess = Session.Session( req, sid=pysid)
>     else:
>         req.sess = Session.Session( req)
>     #req.sess = Session.Session( req)
>     req.sess.set_timeout( 20 * 60)
>     id = req.sess.id()
>     if not req.sess.is_new():
>         f.write( 'old session: ' + id + '\n')
>     else:
>         f.write( 'new session: ' + id + '\n')
>     f.close()
>     return segment.dispatchToHandler( req)
> I know that I do not need to specify the sid attribute of Session for an 
> old session to be reinstated (this can be determined from the request 
> object), but it wasn't working when I did it that way either.  Something 
> that seems very strange to me is that the pysid value I get back from 
> here does not match the cookie that has been set in my browser.  As you 
> can see, I set the timeout to 20 minutes, which is much longer than the 
> time inbetween the 2 calls.  Is there anything else I should try?

In your example code you never save the session data. When you create a 
session instance with an explicit sid, Session tries to load the 
corresponding session. If that session does not exist a new session id 
is created, and thus you see a new pysid on each request.

You need req.sess.save() somewhere in your code.


More information about the Mod_python mailing list