[mod_python] newbie cookie confusion

Sells, Fred fred at adventistcare.org
Wed Oct 11 11:43:01 EDT 2006


Thanks to both Graham and Scott, I had a careless error in my python where I
was getting a list of cookie keys from the dict and not the cookies.  oops
:(

-----Original Message-----
From: Graham Dumpleton ...



Sells, Fred wrote ..
> I am using the mod_python.publisher; could that mess me up with cookies
> somehow?
> I'm just trying to get the basic demo from the manual to work.
> 
> I notice the manual writes a plain cookie but reads a marshall cookie.
> Am I
> correct in assuming these are not compatible cookies, and you had better
> read the type you wrote?

What is the actual problem you are having. Someone already pointed out
what you were probably doing wrong after your first post. That is that
the result of Cookie.get_cookies() is a dictionary. You were not using the
value (cookie) from the dictionary, but the key (name of cookie) which is
a string.

Change your test program to use:

    for c in cookies.values():
        s += '\n%s %s' % (str(c), type(c))

and you will see the actual cookies read back. You still need to access
the name and value attributes of the cookie to get the decoded value.

In terms of the example in the documentation, the two examples should
be seen as distinct and not as working together. In the second example
it itself sets up the MarshalCookie if it wasn't there to begin with.

As far as not getting the examples to work, are you using them exactly
as in the documentation, or have you used it merely as a guide to write
your own code?

In the event that cookies cached in the browser are the cause of any
problems, are you deleting from your browser cache any specific cookie
used by the test code?

Graham

> Sells, Fred wrote:
> > I've googled but not found this, trying to do cookie example from docs.
> The
> > output is always of type <type 'str'> and is the cookie name.  How do
> I
> get
> > value?
> > 
> > 
> > 
> > def makecookie(req):
> >     c = Cookie.MarshalCookie('spam', 'eggs', 'secret007')
> >     d = Cookie.MarshalCookie('xxxx', 'yyy', 'secret007')
> >     Cookie.add_cookie(req, c)
> >     Cookie.add_cookie(req, d)
> >     return 'cookie made'
> > 
> > def showcookies(req):
> >     cookies = Cookie.get_cookies(req, Cookie.MarshalCookie,
> > secret='secret007')
> >     s = 'There are %s cookies'% len(cookies)
> >     for c in cookies:
> >         s += '\n%s %s' % (str(c), type(c))
> >     return s
> > 
> 
> I haven't been through the docs but I'd suggest that you have a look at
> cookies[c] in case cookies is a dictionary.
> 
> Scott



More information about the Mod_python mailing list