Peter's Blog

Redefining the Impossible

TurboGears, cherrypy, mod_python, cookie woes


Had a problem in my Turbogears app in that I was trying to set a cookie. It worked nicely under development, running on the cherrypy server but it didn't work in production when running under mod_python/apache. Eventually I established that it was because my http response was trying to send two cookies (my cookie and the session cookie) and only the last one was actually being sent to the browser.

I pinned it down to the mpcp script that I am using to interface mod_python with cherrypy. It has this code:

elif tv is str:
    req.headers_out[header] = value

which is only allowing one 'Set-Cookie' header to be defined, later ones will overwrite the original.

Changing the code to the following fixes the problem:

elif tv is str:
    if header.lower() != 'set-cookie':
        req.headers_out[header] = value
    else:
        req.headers_out.add( 'Set-Cookie', value)

Stefan Says:

over 2 years ago

Hey Peter,

did you ever try to run multiple TurboGears apps in one Apache/mod_python environment?

I am running into a series of 404 errors and I suspect the mpcp bridge as the culprit (it probably initializes CherryPy in the first request, then throws 404 if a request for a different app comes in, as this app is not configured) ... I'm currently just looking to confirm that that's not just my problem :)

Cheers, Stefan

Peter Says:

over 2 years ago

Yes I did try running multiple applications and no it didn't work.

Seems like a limitation of cherrypy. I haven't tried solving this for many months, too busy on other stuff. It was embarrasing though, I had two web apps ready to roll and had to sideline one.

If I had my time over again I would have done it in Django as that seems to handle multiple applications out-of-the-box. I wasn't impressed with cherrypy, when you see the nasty hacks that are required to get it working and the attitude of the developers to WSGI.

Peter

Have Your Say

I welcome constructive comments or questions but I reserve the right to delete any comments that displease me.

Who are you?

(Optional) If you enter an email address here I might email you back. Your email address will not be sold to spammers or shown anywhere

What do you have to say?