Home arrow Python arrow Page 4 - A Brief Look at Mod_Python

Creating Handlers and Working with the API - Python

Python's Apache interpreter is available as an Apache module, mod_python. This module reduces the time it takes to deliver a given page to a client. It is also capable of a great deal more, including interacting with Apache itself in various powerful ways. This article gives you just a taste of what mod_python can do.

TABLE OF CONTENTS:
  1. A Brief Look at Mod_Python
  2. Installing Mod_Python
  3. Getting Started
  4. Creating Handlers and Working with the API
  5. Python Server Pages
By: Peyton McCullough
Rating: starstarstarstarstar / 20
April 25, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

I stated earlier in the article that it is possible to change the way Apache does things. This is done through handlers. Apache does its work in steps, or phases. Each of these steps can be performed by a handler. Mod_python registers a handler for each step, which can be populated by Python instructions.

This feature of mod_python is rather interesting, and we can do things with it that are equally as interesting. Let's say we want to change the way text documents are viewed, putting the document inside a pretty layout. We can do this by creating a handler that takes over at the stage where content is gathered and delivered to the user. This might sound a bit confusing, so it's best for me to show you rather than tell you. Create a directory named pyhandler and drop this inside its .htaccess file:

AddHandler mod_python .txt
PythonHandler headerfooter

This tells Apache that .txt files are to be handled by mod_python. It then tells mod_python that the file headerfooter.py should be called to handle the .txt files. Create headerfooter.py and add this to it:

from mod_python import apache

def handler ( request ):
  request.content_type = 'text/html'
  request.write ( "<html>" )
  request.write ( "<head>" )
  request.write ( "<title>Text Document</title>" )
  request.write ( "</head>" )
  request.write ( "<body bgcolor='#D2D2D2'>" )
  request.write ( "<table align='center' bgcolor='#000000' cellspacing='1px' cellpadding='5px' width='60%'>" )
  request.write ( "<tr><td bgcolor='#FFFFFF' align='center'>" )
  request.write ( file ( request.filename ).read().replace( '\n', '<br>' ) )
  request.write ( "</td></tr>" )
  request.write ( "</table>" )
  request.write ( "</body>" )
  request.write ( "</html>" )
  return apache.OK

That's quite a mouthful, so let's break it down. The first line imports the module apache. This allows us to interact with Apache. We then define a method, handler, that accepts one parameter, request. The request object contains information about (drumroll please) the request.

The next line's function is a bit more obvious. We send out the content type of the page. We then print out a layout using the write method and print the contents of the text file, whose name is contained in the filename method of the request object. We then tell Apache that everything went fine.



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: