Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got a website that I wrote in python using the CGI. This was great up until very recently, when the ability to scale became important.

I decided, because it was very simple, to use mod_python. Most of the functionality of my site is stored in a python module which I call to render the various pages. One of the CGI scripts might look like this:

#!/usr/bin/python

import mysite

mysite.init()
mysite.foo_page()
mysite.close()

and in mysite, I might have something like this:

def get_username():
     cookie = Cookie.SimpleCookie(os.environ.get("HTTP_COOKIE",""))
     sessionid = cookie['sessionid'].value
     ip = os.environ['REMOTE_ADDR']
     username = select username from sessions where ip = %foo and session = %bar
     return(username)

to fetch the current user's username. Problem is that this depends on os.envrion getting populated when os is imported to the script (at the top of the module). Because I'm now using mod_python, the interpreter only loads this module once, and only populates it once. I can't read cookies because it's os has the environment variables of the local machine, not the remote user.

I'm sure there is a way around this, but I'm not sure what it is. I tried re-importing os in the get_username function, but no dice :(.

Any thoughts?

share|improve this question
add comment (requires an account with 50 reputation)

1 Answer

up vote 0 down vote accepted

Which version of mod_python are you using? Mod_python 3.x includes a separate Cookie class to make this easier (see here)

Under earlier versions IIRC you can get the incoming cookies inside of the headers_in member of the request object.

share|improve this answer
req.headers_in["cookie"] -- Yep, that was it. Thank you! – Ryan Nov 22 '10 at 0:14
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.