[mod_python] (more) Cookies trouble

Wouter van Marle wouter at squirrel-systems.com
Fri May 13 22:45:00 EDT 2005


Well, the below issue is solved: I had to add the statement 'path = "/"'
or 'path = "/python"' when setting the cookie:
Cookie.add_cookie(req,
                  Cookie.MarshalCookie("basic", value, "gezellig",
                                       expires = time.time() + 7776000,
                                       path = "/"
                                       ))
And now it works all nicely. Apparently it started searching with the
original script as path ("/python/usertest.py") instead of
"/python/test.py" which was the path of the called script, and the path
set in the cookie.

Now only the importing issue!

Wouter.

On Fri, 2005-05-13 at 11:45 +0800, Wouter van Marle wrote:
> Hi all,
> 
> Now the following issue, see the scripts below for demonstration of how
> it "works" here.
> test.py/home serves a simple home page, with two hyperlinks (linking
> back to modules within test.py) and a button linking to
> "testuser.py/register". Here also something goes wrong: the language
> session does not persist.
> testuser.register finds there is no session present (no attribute
> 'session' in req present), so calls back to main.get_session(req) to get
> it's session. There the session should be opened, and if not found,
> defaults read from the "basic" cookie that is set previously, it is set
> with path "/python/test.py" according to Mozilla's cookie manager.
> get_session(req) however now appears to be unable to read the "basic"
> cookie, and returns the default value to testuser.register, instead of
> the value read in the cookie.
> 
> ------------------ 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"]
>     # 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
>                                            ))
>     cookies = Cookie.get_cookies(req, Cookie.MarshalCookie,
>                                  secret = "gezellig")
>     req.write("found cookies: "+str(cookies)+"<br>")
>     req.write("home got language: "+language+"<br>")
>     req.write("Language switcher: <a href=./en>en</a>  <a href=./cn>cn</a><br>")
>     req.write("""register: <form action="../../python/testuser.py/register" method="post"><input value="Register" name="Register" type="submit">""")
> 
> 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 = "default" # system default
>     cookies = Cookie.get_cookies(req, Cookie.MarshalCookie,
>                                  secret = "gezellig")
>     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
>     sess["language"] = language
>     return sess
> 
> ------------------ END test.py
> 
> ------------------ BEGIN usertest.py
> from mod_python import apache
> from mod_python import Cookie
> 
> test = apache.import_module("test")
> 
> def register (req):
>     req.content_type = "text/html; charset=utf-8"
>     if not hasattr(req, "session"):
>         req.session = test.get_session(req)
>     req.write("got cookies: %s <br>"% str(Cookie.get_cookies(req, Cookie.MarshalCookie, secret = "gezellig")))
>     req.write("got language: %s <br>"% req.session["language"])
> 
> ------------------ END usertest.py
> 
> 
> _______________________________________________
> 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