mod_snake_epy

mod_snake_epy is a PyMod which allows Python code to be embedded within standard HTML pages. It provides extensive caching by storing the post-processed code across multiple requests. In addition, it contains the ability to store persistant data such as functions and other objects. This greatly speeds up code execution.

Configuration Directives

To load the embedded Python processor, the folowing directive can be placed in the main configuration file, httpd.conf:

    SnakeModule mod_snake_epy.SnakeEPy
    AddType application/x-httpd-epy .epy

This allows filenames with the extention '.epy' to be interpreted as embedded Python scripts. The directives which may be used after loading this module are:

Usage

Within text/html documents, Python can be embedded in a number of ways.

The EPY Object

Interaction between embedded Python code and mod_snake/Apache is done by using the EPY object. This includes things such as setting up CGI style environment, setting persistance, setting headers, and more. ``EPY'' is a global variable in all executed blocks. It can be used like any other object. e.g.: EPY.set_persist(1)

EPY Methods and Attributes

  • environ - A dictionary containing all the key/value CGI variables. This is setup via the setup_cgi() method. cgi.py contains several functions which can take both an environment and a file object. This attribute should be passed to such functions: e.g.: cgi.FieldStorage(environ=EPY.environ,fp=EPY)

  • set_persist( persist) - Set the persistance of data created and used in the code blocks. If persist is true, global objects created during execution will be available during subsequent requests.

  • set_header( header, value) - Set an output header that gets sent back to the remote client, such as ``content-type''.

  • setup_cgi() - Setup CGI environment variables. After calling this method, EPY.environ contains standard CGI key/value pairs.

  • write( data) - write data to the remote client.

Examples

A small example for displaying the time:

    <+
    import time

    EPY.set_persist(1)
    HitCount = 1
    +>

    The current time is: <:time.ctime(time.time()):><BR>
    This page has been hit <:HitCount:> times.
    <|HitCount = HitCount + 1|>