[mod_python] [RFC] mod_python embedded python module

Brent Verner brent at rcfile.org
Wed Feb 7 08:59:53 EST 2001

  First off, Gregory, thanks for writing this nice software :)

  Secondly, after looking into mod_python and mod_snake, I've 
decided that _I_ like mod_python (the base python/apache binding)
much better than mod_snake. Now, I'm not a python programmer, but
there's no better way to learn than to do, so last night I started
hacking up a module/handler to handle documents having embedded
HTML, the feature which drew me toward mod_snake at first.

  What I intend to do with this module is have it:
    1) parse (so to speak) a document containing HTML-embedded python
       code into pure python code. My tentative direction is to:
        a) create a python file that looks like
env = {}
def embedded_output_hook(req):
  env = req.subprocess_env
  req.write( req.uri )
  for i in env.keys():
    req.write( i )
    req.write(""": """)
    req.write( env[i] )
  # for

from an HTML-embedded file of:
<title><?= req.uri ?></title>
  for i in env.keys():
<b><?= i ?>: <?= env[i] ?></b><br>
  # for

       and the module will call embedded_output_hook(req) from 
       the loaded module. Also, I've thought of having calls to
       content_type/auth/cleanup functions which may be defined in
       the document...
    2) save those parsed files to a cache directory, where they'll
       actually be loaded from when the correspondeing HTML-embedded
       file is requested via the handler. Of course I'm stat()ing
       the files to make sure the parsed/cached file is up to date.

That is all fairly simple, from the overview, but I do have a few 
questions before I go too far down the wrong road :):
  1) is this approach sane, or is there a more elegant/efficient
     way to handle this?
  2) To create the pure-python file from the embedded document, I'm
     pretty sure some restrictions will have to be made on the
     location/designation of functions. What is a reasonable set
     of restrictions that can/should be placed on this so the module
     doesn't become a major-hack-of-a-buncha-special-cases :)
  3) I'm using load_module(...) to load the pure-python files. Is
     there any way (a different one?) that will allow the loaded
     module to access globals from the module's environment, i.e.,
     database connections, et. al.
  4) Is there any way to have multile 'AddHandler' types in the same 
     config section? If not, will you accept clean patches to do so?

comments and discussion are requested. I'm going to do this, I just
want to make sure I do something that is maximally useful :) I'll
not be resonding to this thread until the weekend, cause I've got
some real (non-python :() work that _must_ get done to free up my
weekend to get on with this hack.


"We want to help, but we wouldn't want to deprive you of a valuable 
learning experience."

More information about the Mod_python mailing list