Wouter van Marle
wouter at squirrel-systems.com
Thu May 12 22:54:55 EDT 2005
Sometimes I feel so stupid... remembering "no req.write before finishing the cookies", Stripping those, the cookies part at least works within this test module, and seems to work OK in the real page as well. Now the rest of the problems still persist: main.py/home produces a page that a.o. links to user.py/register. user.py imports main.py (import main), and sometimes gets the error message "No module named main". Sometimes it goes fine, and the module is imported and starts running. It seems the include goes well, as long as I to it within a minute or two from loading the home page (main.py/home). If quickly after that I click <reload> in the browser it goes fine, a moment later I get the include error. Also I just noticed that changes made to main.py are not used when including main in user.py, until I restart apache. I got some old wordings that were changed in the source. Some too persistent caching at work here? Wouter. On Fri, 2005-05-13 at 10:31 +0800, Wouter van Marle wrote: > 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. > > Except: > - 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.add_cookie(req, > Cookie.MarshalCookie("basic", value, "gezellig", > expires = time.time() + > 7776000 # 90 days > )) > req.write("Language switcher: <a href=./en>en</a> <a > href=./cn>cn</a><br>") > > def en(req): > if not hasattr(req, "session"): > req.session = get_session(req) > req.session["language"] = "en" > req.session.save() > home(req) > return > > def cn(req): > if not hasattr(req, "session"): > req.session = get_session(req) > req.session["language"] = "cn" > req.session.save() > home(req) > return > > 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: > try: > language = basiccookie.value["language"] > except: > pass > req.write("get_session got language: "+language+"<br>") > sess["language"] = language > return sess > -------------------------- END test.py > > > Wouter. > > > _______________________________________________ > Mod_python mailing list > Mod_python at modpython.org > http://mailman.modpython.org/mailman/listinfo/mod_python > >
|