Python
  Home arrow Python arrow Page 4 - A Brief Look at Mod_Python
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

A Brief Look at Mod_Python
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 20
    2005-04-25


    Table of Contents:
  • A Brief Look at Mod_Python
  • Installing Mod_Python
  • Getting Started
  • Creating Handlers and Working with the API
  • Python Server Pages

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    A Brief Look at Mod_Python - Creating Handlers and Working with the API
    ( Page 4 of 5 )

    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
     

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT