[mod_python] Sessions

Jim Gallacher jpg at jgassociates.ca
Fri Feb 24 08:29:59 EST 2006

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"
         req.write("The session exists\n")


     # don't forget to save your session data
     return apache.OK


More information about the Mod_python mailing list