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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: 4 stars4 stars4 stars4 stars4 stars / 19
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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 source examples are wrongly formatted and so syntactically incorrect. Please...
       · The examples won't work, since the indentation has been lost. Hopefully the author...
       · We have changed the code samples to the proper indentation that the author provided....
       · I installed mod_python in my apache server but apache is not starting now. When I...
       · Nice Intro - thanx!The following example needed a <html> tag, due to Mime...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway