[mod_python] [SPAM] can't set several cookies from the same page

Pierre Beyssac pb+python at fasterix.frmug.org
Wed Aug 30 11:48:22 EDT 2006


I'm a happy mod_python user since... yesterday.

I can't seem to get the following to work: setting 2 cookies from
the same request. The following code is taken from the documentation
for the most part; apparently, it results in only one "Set-Cookie"
header being emitted in the HTTP reply headers.

Initially I just wanted to establish a session + a cookie at the
same time and it didn't work either, apparently for the same reason.

Any idea what I am doing wrong? Am I missing something obvious?

I'm using mod_python 3.2.10, Python 2.4.3, Apache 2.0.59 on FreeBSD 4.11.

from mod_python import apache, Cookie, Session
import time

SECRET = "mYseCret"

def _cookiehandler(req, ident):
    cookies = Cookie.get_cookies(req, Cookie.MarshalCookie,
    if cookies.has_key(ident):
        mycookie = cookies[ident]
        req.write("OK, cookie found: %s\n" % mycookie)
        if type(mycookie) is Cookie.MarshalCookie:
            req.write('Here is what it looks like decoded: %s=%s\n'
                      % (mycookie.name, mycookie.value))
            req.write('Strange, cannot decode...\n')
        value = {'eu_org': ident}
        Cookie.add_cookie(req, Cookie.MarshalCookie(ident, value, SECRET))
        req.write("No cookie found, trying to set one\n")

def handler(req):
    req.content_type = "text/plain"
    _cookiehandler(req, 'id1')
    _cookiehandler(req, 'id2')
    return apache.OK
Generated reply :
Date: Tue, 29 Aug 2006 19:54:28 GMT
Server: Apache/2.0.59 (FreeBSD) mod_python/3.2.10 Python/2.4.3 mod_ssl/2.0.59 OpenSSL/0.9.7d-p1
Cache-Control: no-cache="set-cookie"
Set-Cookie: id1=d68b2820cbded02eea3afc876689f30ae3QGAAAAZXVfb3JndAMAAABpZDEw
Connection: close
Content-Type: text/plain; charset=ISO-8859-1

No cookie found, trying to set one
No cookie found, trying to set one

	Le mieux est l'ennemi du bien -- Unie dans la diversité
Pierre Beyssac	      	    pb at fasterix.frmug.org pb at fasterix.freenix.org
		Free domains: http://www.eu.org/

More information about the Mod_python mailing list