4.7 Using mod_python to Deploy Applications
Description: Using mod_python to Deploy Applications
Keywords: albatross

#
4.7 Using `mod_python` to Deploy Applications

In all of the previous sections you will note that all of the programs used the `Request` class from the `albatross.cgiapp` module. The `albatross.apacheapp` module contains a `Request` class to allow you to deploy your application using `mod_python` 4.1. In this section we will modify the `popview.py` application to deploy it using `mod_python`.

In theory it is possible to change application deployment method from CGI to `mod_python` by changing less than 5 lines of code in the entire application. The complete sample program is contained in the `samples/popview5` directory. Use the `install.py` script to install the sample.

    cd samples/popview5
    python install.py

The new `popview.py` mainline follows.

    from albatross import ModularSessionApp, SessionAppContext
    from albatross.apacheapp import Request
    import popviewlib

    class AppContext(SessionAppContext):

        def open_mbox(self):
            if hasattr(self.locals, 'mbox'):
                return
            self.locals.mbox = popviewlib.Mbox(self.locals.username, self.locals.passwd)

        def read_msg(self):
            if hasattr(self.locals, 'msg'):
                return
            self.locals.msg = self.locals.mbox[int(self.locals.msgnum) - 1]
            self.locals.msg.read_body()

    class App(ModularSessionApp):

        def __init__(self):
            ModularSessionApp.__init__(self,
                                       base_url='popview.py',
                                       module_path='-=-install_dir-=-',
                                       template_path='-=-install_dir-=-',
                                       start_page='login',
                                       secret='-=-secret-=-',
                                       session_appid='popview5')

        def create_context(self):
            return AppContext(self)

    app = App()

    def handler(req):
        return app.run(Request(req))

The `handler()` function is called by `mod_python` when a browser request is received that must be handled by the program.

You also need to create a `.htaccess` file to tell Apache to run the application using `mod_python`.

    DirectoryIndex popview.py
    SetHandler python-program
    PythonHandler popview

Assuming you install the popview sample below the `/var/www` directory you will need configure Apache settings for the `/var/www/alsamp` directory:

    <Directory /var/www/alsamp/>
        AllowOverride FileInfo Indexes
        Order allow,deny
        Allow from all
    </Directory>

#### Footnotes

...mod_python 4.1
  For more information on `mod_python`, including installation instructions, see http://www.modpython.org/.