Showing posts with label Mod_Python. Show all posts
Showing posts with label Mod_Python. Show all posts

Sunday, August 28, 2011

Install Django+Mod_Python on Ubuntu 10.04

Install general dev tools
sudo apt-get install build-essential devscripts flex 
where flex is essential to compile mod_python.
Install Apache
sudo apt-get install apache2 apache2-threaded-dev
where apache2-threaded-dev is used to compile mod_python.
Install MySQL
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
where libmysqlclient-dev is used to install Python MySQL library. During the installation, you will be asked to set a password for root.
Install Python 2.6
sudo apt-get install python2.6 python2.6-doc python2.6-dev python-setuptools
where python2.6 maybe omitted if it already installed (It's installed with Ubuntu 10.04). After installed python-setuptools, you will be able to install python modules with easy_install.
Install mod_python
svn co https://svn.apache.org/repos/asf/quetzalcoatl/mod_python/trunk/
cd trunk
./configure --with-python=/usr/bin/python2.6
make
sudo make install
If ./configure fails, check if you missed anything above. To enable the mod_python in apache2, add following to /etc/apache2/httpd.conf
LoadModule python_module /usr/lib/apache2/modules/mod_python.so
Install memcached
sudo apt-get install memcached

Wednesday, August 24, 2011

An environment variable setting issue for python os.system method (Windows)

Yesterday, I encountered an interesting problem about environment settings for Python os.system method. I tried to run a shell command "msgfmt -o file.mo file.po" in python. Msgfmt.exe locates in D:\MinGW\bin, and it's added in the environment variable PATH for Administrator. When I call the following command in the python interactive shell:
os.system("msgfmt -o file.mo file.po")
It works perfectly. When I run the python file containing this line of codes, it runs perfectly too. However, when it is loaded as a module in Mod_Python's psp file, it doesn't work anymore and fails silently. After investigation, it turns out that the msgfmt is not recognized by Mod_Python as an executable command. Because window cmd shell is default to be running as Administrator, and PATH for Administrator works for Python while Mod_Python is not running as Administrator which caused this problem. Therefore adding D:\MinGW\bin into system variables PATH solved this problem perfectly.

Monday, August 1, 2011

Mod_Python: "internal_redirect" tricks



1. Access the request object from which it was redirected by req.prev, cause Apache/Mod_Python constructs a new request object when handling an internal_redirect() call.
2. Pass information through req.internal_redirect(), for example, a handler which contains:
import os 
def handler(req): 
    req.subprocess_env.add("XXX","YYY") 
    req.internal_redirect(os.path.split(req.uri)[0]+'/page') 
    return apache.OK
And a second handler which is triggered as the target of the redirect which contains:
from mod_python import apache 
def handler(req): 
    req.content_type = "text/plain" 
    for key in req.subprocess_env.keys(): 
        print >> req, key, "=", req.subprocess_env[key] 
    return apache.OK