[mod_python] Cookies and sessions trouble

Wouter van Marle wouter at squirrel-systems.com
Thu May 12 22:31:49 EDT 2005

Hi all,

As described in a previous thread (" mod_python session trouble") I've
had some problems with the sessions. Now there is some progress, though
not really great...
My website is run from a set of python scripts, serving up different
pages, pretty much all from psp templates. To keep track of the user,
I'm using a session (for current session info) and a more persistent
cookie for longer term storage called "basic".

Now what I found:
- the homepage is run from "main.py", together with a few other pages.
Within these pages the session is passed on nicely, and if that doesn't
work, the "basic" cookie is accessed for the information.
- main.py contains a function (get_session(req)) to read the session,
and if expires read the cookie. Works nicely. Returns the session.

- when calling "req.session main.get_session(req)" from my script
user.py (via a hyperlink in the homepage) I get all kinds of errors,
that are not consequent! Sometimes it complains it can not find the very
function, another time it complains about unbound variables in this
function, sometimes it runs and returns a session. Reloading the
homepage and then moving to one of the subpages served by the other
script (user.py).
- when trying to read the cookies from user.py, I can not get my
original cookies. The session appears to be new all the time
(req.session.is_new returns "True"), and I can not read by "basic"
cookie that is happily visible from functions that are in main.py. As a
result I can not read the user's session settings, nor their defaults,
and the script falls back to system defaults.

I've also done some testing, and here a small script showing part of the
strange behaviour.
When starting up the very first time, you have no cookies (makes sense),
and "language" is set to system default "en". However if you start
switching it through the links, the language is NOT saved in the
session, nor a "basic" cookie is set at all. This cookie is supposed to
be set in the home function, to store the current user settings for
later use.

----------------- BEGIN test.py
from mod_python import Cookie
from mod_python import Session

import time

def home (req):
    req.content_type = "text/html; charset=utf-8"
    username = ""
    if not hasattr(req, "session"):
        req.session = get_session(req)
    language = req.session["language"]
    req.write("home got language: "+language+"<br>")
    # update the cookie on the user's side
    value = {"language": language, "username": username}
                      Cookie.MarshalCookie("basic", value, "gezellig",
                                           expires = time.time() +
7776000 # 90 days
    req.write("Language switcher: <a href=./en>en</a>  <a

def en(req):
    if not hasattr(req, "session"):
        req.session = get_session(req)
    req.session["language"] = "en"

def cn(req):
    if not hasattr(req, "session"):
        req.session = get_session(req)
    req.session["language"] = "cn"

def get_session(req):
    # gets the current session, checks if complete, if not reads
defaults from
    # the cookies.
    sess = Session.Session(req)
    req.content_type = "text/html; charset=utf-8"
    # read user's settings from their cookies
    language = "en" # system default
    cookies = Cookie.get_cookies(req, Cookie.MarshalCookie,
                                 secret = "gezellig")
    req.write("found cookies: "+str(cookies)+"<br>")
    if cookies.has_key("basic"):
        # the basic info is stored in a cookie named "basic"
        basiccookie = cookies["basic"]
        if type(basiccookie) is Cookie.MarshalCookie:
                language = basiccookie.value["language"]
    req.write("get_session got language: "+language+"<br>")
    sess["language"] = language
    return sess
-------------------------- END test.py


More information about the Mod_python mailing list