[mod_python] Cookie problems

Sean Jamieson sean at barriescene.com
Fri Jan 27 12:59:58 EST 2006

First, there cookies are supposed to be limited to 1 or 2KB MAX, this 
limit is enfoced by the browser, and therefore very subjective. You 
should not expect to be able to set more than 2kb for the entire set of 
cookies. If you are able to see more, then its the browser being 
non-standard :-)


Wouter van Marle wrote:

>Hi All,
>I'm fiddling around with cookies, and after wondering why I always lost
>my settings information I did some testing, and ran into what seems a
>very very strict limit in cookie length. The code here is based on the
>examples of the web pages. From the documentation I find that there is
>no strict limit on the size of the value in the cookies, the only limit
>I found mentioned in the archives is a 4k http header limit from Apache.
>I'd assume I'm way below that.
>Test 1 works correctly.
>Test 2, with a slightly larger data for the cookie, fails. It is for
>some reason not recognised as a valid MarshalCookie! The value used in
>this case (the rest of the code being identical):
>value = {'egg': 32,
>         'color': 'white',
>         'foo': 'bar',
>         'foobar': 5}
>Here the code of Test 1 (maybe with some extra line breaks due to e-mail
>from mod_python import Cookie, apache
>import time
>def handler(req):
>    req.content_type = "text/html; charset=utf-8"
>    cookies = Cookie.get_cookies(req, Cookie.MarshalCookie,
>                                    secret='secret007')
>    if cookies.has_key('spam'):
>        spamcookie = cookies['spam']
>        req.write('Great, a spam cookie was found: %s\n' \
>                                      % str(spamcookie))
>        if type(spamcookie) is Cookie.MarshalCookie:
>            req.write('Here is what it looks like decoded: %s=%s\n'
>                      % (spamcookie.name, spamcookie.value))
>        else:
>            req.write('WARNING: The cookie found is not a \
>                       MarshalCookie, it may have been tapered with!')
>    else:
>        value = {'egg': 32,
>                 'color': 'white',
>                 'foo': 'bar'}
>        Cookie.add_cookie(req, Cookie.MarshalCookie('spam', value, 'secret007'))
>        req.write('Spam cookie not found, but we just set one!\n')
>    return
>Mod_python mailing list
>Mod_python at modpython.org

More information about the Mod_python mailing list