[mod_python] Cookies

Chris Hagner CHagner at fool.com
Thu Jul 26 11:11:54 EST 2001


There's a module called Cookie which provides an Dictionary-like interface
to access the multiple cookies in a request.  In addition, each cookie class
has an output method that returns an HTTP-header-friendly string
("Set-Cookie: fig=newton").  The doc strings in Cookie.py give some pretty
good code examples.

In this module, a 'raw' cookie is called a morsel, so the terminology could
be a little confusing.

One warning though.  The Cookie module is only designed to handle a single
value for each cookie (i.e. "Set-Cookie: MyCookie=MyValue") .  Thus, if you
use a cookie to store more than one value (i.e. "Set-Cookie:
MyCookie=key1=value=&key2=value2&key3=value3"), you'll need to wrap Cookie
with your own class to handle the 'internal' values (or just build/parse the
value string yourself each time).

Chris

-----Original Message-----
From: Bo Lorentsen [mailto:bl at netgroup.dk]
Sent: Thursday, July 26, 2001 10:54 AM
To: riesch at nieuw.nl
Cc: mod_python at modpython.org
Subject: Re: [mod_python] Cookies


riesch at nieuw.nl wrote:

> I'm currently making a web-site with mod-python and since I want to make
use of cookies, I was wondering how I can set a cookie.
> I've been looking through the documentation provided on the web-site, but
I'm not able to find something. So if anyone could give me pointer, or even
better comething that works, I'd be much obliged.

You find them in the header fields (headers_in / headers_out ) in the
Request class.

If you like to set a cookie you do something like this :

    req.headers_out[ "Set-Cookie" ] = "my_session_id=42"

and the next time this user sends back a request back to you ( and if he
support it) it will contain it in your input headers, and you will be able
to fetch it like this :

    raw_cookie = req.headers_in[ "Cookie" ]

Now you only need to parse the input cookie string (raw_cookie), that have
the syntax as descried in RFC-2109.

mod_python does not have any helper classes for either in- or outgoing
cookies, but the format is strait forward, even I could make one :-)

Hope this helped

Regards

/BL

_______________________________________________
Mod_python mailing list
Mod_python at modpython.org
http://www.modpython.org/mailman/listinfo/mod_python



More information about the Mod_python mailing list