[mod_python] question - Getting/Setting Cookies

perry.tew at cibavision.novartis.com perry.tew at cibavision.novartis.com
Thu Feb 12 12:04:09 EST 2004

First, sorry for posting redundant information about how to handle 
cookies.  I hadn't received everyone's posts until after I sent mine.

Second, as far as cookies go, I've used SimpleCookie.   I see that Grisha 
recently added a Cookie class specific to mod_python.
Let me ask, has any investigation been done into libapreq?  I know it 
seems to be a mod_perl thing, but on their main page they have a blurb 
about python tie-ins with nothing listed below.

I ask of speed issues.
Using python code to parse cookies is pretty expensive.  Not horrible, but 
when every request is parsed for cookies it becomes a significant part of 
processing time, especially with a lot of cookies being set and passed 
around.  For an authentication handler, I've been able to reduce this time 
by slicing the cookie header for just my cookie and then passing that to 
the cookie load() method.  It cut my time to 1/5 when I had a lot of 

                cookie_header = req.headers_in.get('Cookie', '')
                start_index = string.find(cookie_header, 'Ticket')
                if start_index == -1:
                        return 0, "user has no ticket cookie", 'noone'

                end_index = string.find(cookie_header, ';', start_index)
                if end_index == -1:
                        end_index = len(cookie_header)
                stripped_down_cookie = 

                cookie = Cookie.SimpleCookie()
                ticket = cookie['Ticket'].value

The above code is much quicker than passing the entire header to the 
load() when there are many cookies.  btw, if someone has some optimization 
tips for the above code, or a better way to do it, please let me know!

At any rate, I haven't tested Grisha's new cookie class, but it's also a 
pure python solution.   That's what got me thinking about libapreq since 
it's a C solution and has routines for parsing cookies.   Thought I'd ask 
if anything has been done yet.   I googled and came up blank.  Anyone?

Perry Tew

# I use this
cookies = SimpleCookie()
cookies['cookie1'] = value1
cookies['cookie2'] = value2
req.headers_out.add('Set-Cookie', cookies.output(header='', 

Bud P. Bruegger wrote:

> Hi David,
> I don't know much about mod-python--just planning to use it myself. 
> But maybe this is what you're looking for:
> http://www.modpython.org/live/mod_python-3.1.2b/doc-html/pyapi-cookie.html 
> I came across this before and had a hell of a time finding it again. 
> Trouble seems to be that it is in the 3.1 beta version and the home 
> page links to the stable manual (3.0).
> cheers
> -b
> At 17.26 12/02/2004 +1300, you wrote:
>> Hi,
>> I've been a longtime user of Cookie.SimpleCookie, as an easy way of 
>> getting/setting cookies within Python CGI scripts.
>> A couple of days ago, I installed mod_python.
>> While I'm impressed with the performance gain mod_python is giving me 
>> with its CGI handler, I suspect things can run faster still if I 
>> write my own handler.
>> Only problem is - I can't see a trivial way to get a Cookie object 
>> from the 'req' object which gets passed to one's mod_python handler.
>> So my question is - is there any code for getting a Cookie object 
>> from the 'req' object?
>> It's not too hard to write one - just a bit of farting about. But I'd 
>> rather not 'reinvent a wheel' if someone else has already done this.
>> But if I have to write it myself, here's another question - how do I 
>> get the raw http headers from 'req' as a bulk string, which I can 
>> pass to Cookie.load()?
>> Thanks in advance for your help
>> -- 
>> Kind regards
>> David
>> -- 
>> leave this line intact so your email gets through my junk mail filter
>> _______________________________________________
>> Mod_python mailing list
>> Mod_python at modpython.org
>> http://mailman.modpython.org/mailman/listinfo/mod_python

> Ing. Bud P. Bruegger, Ph.D.                 bud at comune.grosseto.it
> Servizio Elaborazione Dati                    0564-488 577 (voice)
> Comune di Grosseto                            0564- 21139 (fax)
> Via Ginori, 43
> 58100 Grosseto
> Collaborazione Open Source per la CIE http://www.comune.grosseto.it/cie/
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

Mod_python mailing list
Mod_python at modpython.org

More information about the Mod_python mailing list