\chapter{Introduction\label{introduction}}

\section{Performance\label{intr-performance}}

  One of the main advantages of mod_python is the increase in
  performance over traditional CGI. Below are results of a very crude
  test. The test was done on a 1.2GHz Pentium machine running Red Hat
  Linux
  7.3. \citetitle[http://httpd.apache.org/docs-2.0/programs/ab.html]{Ab}
  was used to poll 4 kinds of scripts, all of which imported the
  standard cgi module (because this is how a typical Python cgi script
  begins), then output a single word \samp{Hello!}. The results are
  based on 10000 requests with concurrency of 1.

\begin{verbatim}

    Standard CGI:               23 requests/s
    Mod_python cgihandler:     385 requests/s
    Mod_python publisher:      476 requests/s
    Mod_python handler:       1203 requests/s

\end{verbatim}         

\section{Flexibility\label{intr-flexibility}}

  Apache processes requests in phases (e.g. read the request, parse
  headers, check access, etc.). These phases can be implemented by
  functions called handlers. Traditionally, handlers are written in C
  and compiled into Apache modules. Mod_python provides a way to
  extend Apache functionality by writing Apache handlers in
  Python. For a detailed description of the Apache request processing
  process, see the \citetitle[http://dev.apache.org/API.html]{Apache
  API Notes}, as well as the
  \citetitle[http://www.modpython.org/python10/]{Mod_python -
  Integrating Python with Apache} paper.

  To ease migration from CGI, a standard mod_python handler is
  provided that simulates the CGI environment allowing a user to run
  legacy scripts under mod_python with no changes to the code (in most
  cases).
 
\begin{seealso}
  \seeurl{http://dev.apache.org/}{Apache Developer Resources}
  \seeurl{http://www.modpython.org/python10/}{Mod_Python - Integrating Python with Apache, presented at Python 10}
\end{seealso}

\section{History\label{intr-history}}

  Mod_python originates from a project called
  \citetitle[http://www.ispol.com/home/grisha/httpdapy/]{Httpdapy}
  (1997). For a long time Httpdapy was not called mod_python because
  Httpdapy was not meant to be Apache-specific. Httpdapy was designed
  to be cross-platform and in fact was initially written for the
  Netscape server (back then it was called Nsapy (1997).

  This excerpt from the Httpdapy README file describes well the
  challenges and the solution provided by embedding Python within the
  HTTP server:

\begin{verbatim}

While developing my first WWW applications a few years back, I found
that using CGI for programs that need to connect to relational
databases (commercial or not) is too slow because every hit requires
loading of the interpreter executable which can be megabytes in size,
any database libraries that can themselves be pretty big, plus, the
database connection/authentication process carries a very significant
overhead because it involves things like DNS resolutions, encryption,
memory allocation, etc.. Under pressure to speed up the application, I
nearly gave up the idea of using Python for the project and started
researching other tools that claimed to specialize in www database
integration. I did not have any faith in MS's ASP; was quite
frustrated by Netscape LiveWire's slow performance and bugginess; Cold
Fusion seemed promising, but I soon learned that writing in html-like
tags makes programs as readable as assembly. Same is true for
PHP. Besides, I *really* wanted to write things in Python.

Around the same time the Internet Programming With Python book came
out and the chapter describing how to embed Python within Netscape
server immediately caught my attention.  I used the example in my
project, and developed an improved version of what I later called
Nsapy that compiled on both Windows NT and Solaris.

Although Nsapy only worked with Netscape servers, it was a very
intelligent generic OO design that, in the spirit of Python, that lent
itself for easy portability to other web servers.

Incidently, the popularity of Netscape's servers was taking a turn
south, and so I set out to port Nsapy to other servers starting with
the most popular one, Apache. And so from Nsapy was born Httpdapy.

\end{verbatim}

  ...continuing this saga, yours truly later learned that writing
  Httpdapy for every server is a task a little bigger and less
  interesting than I originally imagined.

  Instead, it seemed like providing a Python counterpart to the
  popular Perl Apache extension mod_perl that would give Python users
  the same (or better) capability would be a much more exciting thing
  to do.

  And so it was done. The first release of mod_python happened in May
  of 2000.

