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.
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:
SnakeEPy on|off - This directive takes one flag, 'on' or 'off', and signifies whether or not processing of embedded Python code should be performed.
SnakeEPyErrSend on|off - During the debugging stage of embedded Python, it is sometimes useful to send traceback information directly to the browser instead of to the logs. If this directive is 'on', tracebacks will be dumped to the browser on failure.
SnakeEPyLogFile path - If logging other than the ErrorLog is required, this directive specifies a file where such information should go.
Within text/html documents, Python can be embedded in a number of ways.
<+ ... Python code ... +> - The ``plus'' delimiters designate a section of Python code which should be executed ONLY on the first time the script is loaded. This provides a convenient place to create database connections, open logs, or import other modules.
Warning |
If other code blocks require objects created in the ``plus''delimiter, persistance will be required. See the section called EPY Methods and Attributes. |
<| ... Python code ... |> - The ``pipe'' delimiters designate a section of Python code which should be executed, and the resultant data discarded.
<: ... Python code ... :> - The ``colon delimiters designate a section of Python code which should be evaluated, and the resultant data sent to the remote client.
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)
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.
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|> |