[mod_python] Exposing additional C APIs

Graham Dumpleton graham.dumpleton at gmail.com
Sun Apr 22 06:03:28 EDT 2007

On 22/04/07, python at longlost.info <python at longlost.info> wrote:
> Hi,
> I've written an apache module encapsulating a data structure and ideally I would like to expose the API to scripts running within mod_python.  My guess is that there is no way to do this right now - but it can't hurt to ask, right?
> I was looking into the 'providers' API in the apache portability runtime.  Maybe mod_python could be extended to with a PythonImportProvider directive which allows other apache modules to import their Python API into a python interpreter?  Similar to the existing PythonImport directive.
> Thanks in advance for any help/thoughts,

There are a couple of ways you can go about it, although it is very
much virgin territory for development and using parts of mod_python
that aren't really officially designated public, but more for
experimentation only. Thus these parts of mod_python can possibly
change in the future.

First is that mod_python exports hooks which can be used to
acquire/release designated Python interpreter instances and get hold
of the request object for a request_rec. A separate Apache module can
use these to get to the request object and set an attribute of the
request object to some externally created Python object.

For this approach, see:


The second way of possibly going about it is to use the fact that
there are Python CObject references for the underlying Apache
request_rec structure available in the Python request object as
'_request_rec' attribute. This could be passed into a custom Python
module which uses the request_rec to get access to some data
associated with a different Apache module and then returns a Python
object which wraps that data or translates it into a Python only
object. This custom module could be created by hand or could be
generated using SWIG in some way.

I have previously posted an example that does a similar thing before,
although it predates the '_request_rec' attribute so could be done in
better ways now. For this see:


FWIW, I have Python SWIG bindings for most of the Apache and Apache
Runtime Libraries which I have been playing with. It is probably
somewhat impractical to utilise these in any way in mod_python itself
now due to the way the code was developed, but am looking at making it
possible to hook certain phases of Apache in mod_wsgi with everything
in Python side then being done using the Python SWIG bindings. Also
looking at the concept of being able to develop true Apache modules in
Python, including addition of configuration directives. This though
will all be independent of mod_python though.


More information about the Mod_python mailing list