Jim Gallacher
jpg at jgassociates.ca
Wed Aug 30 16:44:44 EDT 2006
Your first call to req.write() causes the response headers to be sent to the client. Once the headers are sent it's too late to set another cookie. This is not unique to mod_python - it's just the way cookies work. Make sure you set all your cookies (or any other header for that matter) before calling req.write() Jim Pierre Beyssac wrote: > Hello, > > 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, > secret=SECRET) > 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)) > else: > req.write('Strange, cannot decode...\n') > else: > 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 >
|