Mod python

From ArchWiki
Revision as of 16:45, 8 July 2008 by Rbreznak (Talk | contribs) (New page: Category:Networking (English) Category:HOWTOs (English) == Introduction == Mod_python is an Apache module that embeds the [http://www.python.org Python] interpreter within the...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Introduction

Mod_python is an Apache module that embeds the Python interpreter within the server. With mod_python you can write web-based applications in Python that will run many times faster than traditional CGI and will have access to advanced features such as ability to retain database connections and other data between hits and access to Apache internals. A more detailed description of what mod_python can do is available in this O'Reilly article.

Installation

Apache Module : Mod_Python

This document describes how to set up and test the Apache module Mod_Python on an Arch Linux system.

Install Package

# pacman -Sy mod_python

Configure Apache

  • Add this line to /etc/httpd/conf/httpd.conf:
LoadModule python_module /usr/lib/apache/mod_python.so
  • Restart Apache
# sudo apachectl restart
  • Check to make sure that Apache loaded correctly

Test Mod_Python

  • Add this block to /etc/httpd/conf/httpd.conf:
<Directory /home/www/html> 
   AddHandler mod_python .py
   PythonHandler mod_python.publisher 
   PythonDebug On 
</Directory>
  • Create a file in /home/www/html/ called mptest.py and add this as contents:
from mod_python import apache
def handler(req):
    req.content_type = 'text/plain'
    req.send_http_header()
    req.write("Hello World!")
    return apache.OK
  • Restart Apache
# sudo apachectl restart
  • Check to make sure that Apache loaded correctly
Hello World!

With the configuration written above, you can also point your browser to any URL ending in .py in the test directory. You can for example point your browser to /foobar.py and it will be handled by mptest.py.

See Also