[mod_python] Chewing cookies

matt matt at proweb.co.uk
Mon Jul 22 10:10:56 EST 2002

----- Original Message -----
From: "vio" <vmilitaru at sympatico.ca>
To: <mod_python at modpython.org>
Sent: Thursday, July 04, 2002 7:53 PM
Subject: [mod_python] Chewing cookies

> 'REQUEST.headers_out.add('Set-Cookie',cookie_value)' seems the recommended
way to set up multiple cookies on the client. But what is the recommended
way to get back at those cookies? Seems to me that anything like
'REQUEST.headers_in['Set-Cookie'] is no good with more than one cookie, for
obvious reasons. Any suggestions?

cookies are sent to the server url encoded and semi-colon separated on one

Cookie: foo=f00%20foo; bar=bar34; bazz=%56;

# here's a noddy cookie cracker

import urllib

cookie_string = REQUEST.headers_in['Cookie']
# eg. cookie_string = 'foo=f00%20foo; bar=bar34; bazz=%37%38%58%58;'

cookies = {}

for cookie in cookiestring.split(";") :
    key_value = cookie.split("=")

 if len(key_value) == 2 :
    cookies[urllib.unquote(key_value[0].strip())] =
print cookies

# eg. outputs
# {'bazz': '78XX', 'foo': 'f00 foo', 'bar': 'bar34'}

# matt

More information about the Mod_python mailing list