我有一个使用 CGI 用 python 编写的网站。直到最近,当扩展能力变得重要时,这一切都很棒。
我决定使用 mod_python,因为它非常简单。我网站的大部分功能都存储在一个 python 模块中,我调用该模块来呈现各个页面。 CGI 脚本之一可能如下所示:
#!/usr/bin/python
import mysite
mysite.init()
mysite.foo_page()
mysite.close()
在 mysite 中,我可能有类似这样的内容:
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)
获取当前用户的用户名。问题是,这取决于将 os 导入到脚本(位于模块顶部)时填充 os.envrion。因为我现在使用 mod_python,所以解释器仅加载该模块一次,并且仅填充一次。我无法读取cookie,因为它的操作系统具有本地计算机的环境变量,而不是远程用户的环境变量。
我确信有办法解决这个问题,但我不确定它是什么。我尝试在 get_username 函数中重新导入 os,但没有骰子:(。
有什么想法吗?
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?
发布评论
评论(1)
您使用的是哪个版本的 mod_python? Mod_python 3.x 包含一个单独的
Cookie
类,以简化此操作(请参阅 此处)在早期版本 IIRC 下,您可以在请求对象的
headers_in
成员中获取传入的 cookie。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.