Browse DevX
Sign up for e-mail newsletters from DevX


A Brief Introduction to Apache's mod_python Module : Page 2

Giving Apache the ability to run Python scripts opens up a wealth of options for eliminating those old CGI scripts and using Python for your Web development tasks—and gives your scripts a big performance boost into the bargain!




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Installing mod_python
Installing mod_python is simple. You typically don't have to compile mod_python because most modern Linux and FreeBSD distributions include the module in their package management systems. For example, here's the command to install mod_python on Ubuntu/Debian:

apt-get install libapache-mod-python2.4

It's just as simple on Redhat / Fedora Core 4:

yum install mod_python

On FreeBSD, the command is:

cd /usr/ports/www/mod_python3; make install clean

After installing mod_python you must configure Apache to use it. The first step is to enable the module in Apache by adding a LoadModule line to your httpd.conf file. You can just append the LoadModule entry to the end of the other LoadModule entries already in the file.

LoadModule python_module modules/mod_python.so

Author's Note: You may have to change the paths to your particular installation. Ensure that the path you are using is the actual path to the mod_python.so file.

Now that Apache has been configured to load the module, you need to configure a mod_python handler to work with your scripts. Mod_python uses three standard handlers to work with your application. They are the Publisher, PSP (Python Server Pages), and CGI Handlers.

The Publisher Handler
From the mod_python documentation the Publisher is the suggested handler to use when writing new mod_python applications. To configure a Web-accessible directory for use with the Publisher handler you would do the following. This of course assumes that /var/www/html is your Apache document root.

<Directory /var/www/html/python/> SetHandler mod_python PythonHandler mod_python.publisher PythonDebug On </Directory>

The preceding configuration would add the mod_python handler and associate the /var/www/html/python directory to it. This means that all files under the directory will be processed by the mod_python handler. The addition of the PythonHandler means that you now have a single point of entry for Python script processing. The last line enables debugging during development via the PythonDebug On directive.

The Publisher handler requires slightly different code to generate the example getmonth script. You can use the same HTML as in the CGI example, but you must change the action attribute of the <form> tag to:

<form method="POST" action="python/getMonth">

The mod_python code would look like:

# A simple script to output a calendar month based off input # from a web form. # import calendar from mod_python import apache def getMonth(req,month): req.write(calendar.month(2005, int(month),2,3))

Authors note: You do not have to send the content-type headers. Mod_python will handle this for you.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date