Monday, December 12, 2011

Mod_Python tutorial


Mod_python is an Apache module that embeds the Python interpreter within the server. Mod_python is an Apache HTTP Server module that integrates the Python programming language into the Apache server. It is intended to replace Common Gateway Interface (CGI) as a method of executing Python scripts on a web server. The promised benefits are faster execution speed and maintaining data over multiple sessions.

Benefits of Mod_Pyhton

Ordinary CGI execution starts a new process every time a connection is made to a script on the server. This works well in low to medium traffic sites, but does not scale well when used on a high traffic site. This is exactly what mod_python is designed to do. Since the process is not terminated after the script execution like CGI, persistent data can be stored, such as database connections. This may reduce script execution overhead.

Since mod_python modules are integrated with the web server, they can do almost anything an Apache module written in C can do: implement protocols besides HTTP, filter the request and response, determine a document's content type, etc.

Another advantage to mod_python is that it can talk directly to Apache's core system, and interface as well as actually control the heart of Apache. This is done by handlers that the programmer specifies in the Apache configuration file, and every client request directed at that specific directory will not be processed by Apache, rather by mod_python and a "Main" function that handles all requests.

Install Apache with mod_python
For installation process you can visit following link

I'm using Apache 2 and mod_python 3.1.3. After installation you need to configure Apache to load the mod_python module. This is done in the Apache configuration file which usually is httpd.conf

LoadModule python_module /usr/lib/apache2/modules/

You also need to tell mod_python where your Python application is.

Alias /python /var/www/python

SetHandler python-program
PythonHandler application
# Disable these in production
PythonDebug on
PythonAutoReload on

Write your application

The following code is saved in /var/www/python/ The code demonstrates how to use the mod_python API to manipulate the request and response.
from mod_python import apache, util """ Simple mod_python handler. That demonstrates how to use mod_python's API. """  def handler(request):          # Add HTTP headers to the response     request.headers_out.add("version", "0.1")      # Set content type     request.content_type = "text/plain"         # Send headers to browser     request.send_http_header()      # Get request parameters     parameters = util.FieldStorage(request)          # Send a HTTP redirect     if parameters.get("redirect", None) == "true":         util.redirect(request, location = "", permanent = False, text = "Resource found")          # Read request parameter named url.     url = parameters.get("url", "parameter not found")          # Read HTTP header from request     connectionHeader = request.headers_in.get('Connection')          # Write Hello world to the browser     request.write("Hello world")      # Send HTTP 200     return apache.OK 
Test your application

You can access your web application at http://localhost/python/application. You should see Hello World in your browser.

Gregory Trubetskoy has written a paper about mod_python. He uses the Publisher and PSP modules which are part of mod_python. This results in more readable code. Which more resemble Servlets and JSPs in Java.

No comments:

Post a Comment