[mod_python] Sessions

Sebastjan Trepca trepca at gmail.com
Sat Feb 25 21:09:32 EST 2006


FYI, you always have to save the session, even if you are only reading
it, because if you'll not the last accessed property of the session
will not change causing a timeout on active user.

I only call save() when session is more then timeout/2 old(not while
i'm editing of course). How do you guys handle this?

Sebastjan

On 2/24/06, Jim Gallacher <jpg at jgassociates.ca> wrote:
> marinus van aswegen wrote:
> > Hi I have a question re. session handling.
> >
> > Do you need to inform the request handler each time to connect to it which
> > session is calling in?
>
> Your question is a little unclear, but if it means what I think it means
> then the answer is "no". :)
>
> Sessions make use of a cookie. The setting and reading of that cookie is
> handled transparently for you by the session code, so it's pretty simple.
>
> All the magic happens when you create your session instance. If a
> session cookie is found in the request, the corresponding session data
> will be read from the persistent store. If a session cookie is not found
> in the request, a new session id, which will be used as the cookie
> value, will be generated.
>
> from mod_python import apache, Session
> def handler(req):
>      req.content_type = 'text/plain'
>      sess = Session.Session(req)
>      if sess.is_new():
>          req.write("It's a new session!")
>          # initialize your session data
>          sess['stuff'] = "hello world"
>      else:
>          req.write("The session exists\n")
>
>      req.write(sess['stuff'])
>
>      # don't forget to save your session data
>      sess.save()
>      return apache.OK
>
> Jim
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>



More information about the Mod_python mailing list