[mod_python] converting site from CGI to mod_python

Graham Dumpleton graham.dumpleton at gmail.com
Sat Nov 1 04:51:58 EDT 2008

2008/11/1 Adam <adam at fastmail.com.au>:
> Hi,
> I've made a CGI web app for managing DNS zones. I want to convert my scripts
> so they use mod_python instead of cgi, but I can't really find any good
> examples which demonstrate a standard way of doing what I want.
> So everything to do with my website is stored in this directory:
> /var/www/localhost/BDNS
> This directory has three subdirectories.
>  -config
>  -content
>     -templates
>  -scripts
> config  - holds user and zone configuration
> content - originally stored CGI files and templates.
> scripts - stored the scripts to handle dns updates and manage configuration
> Now I'm wondering whether this directory structure should be reognanised if
> I want to use mod_python. The issue is that my documentroot in apache is
> configured to  /var/www/localhost/BDNS/content and I cannot refer to my
> config files in mod_python using relative path names. This was not an issue
> when I was using CGI.
> Consider this as a simple example. I have a file index.py which wants to
> open a config file in the config directory.
> --------------------index.py--------------------
> from mod_python import apache
> bdns = apache.import_module("../scripts/bdns.py")
> tmplutils = apache.import_module("../scripts/tmplutils.py")
> import sys
> def index(req):
>       f = open("../config/bdns.conf")


  conffile = os.path.join(os.path.dirname(__file__), "..", "config",
  f = open(confile, 'r')

If converting from CGI, you may find it better to translate your CGI
script to WSGI instead of directly to mod_python. By using WSGI, you
can still run it as CGI using a CGI-WSGI bridge, but also run your
script under mod_python (with adapter), mod_wsgi (directly), fastcgi
(flup adapter), scgi (flup adapter) etc etc.

In other words, by using WSGI you retain portability and with WSGI not
being too different to CGI in some respects, possibly easier to port.


>       return f.readlines()
> --------------------------------------------------
> This code above doesn't work when I browse to index.py. I don't want to use
> fixed path names.
> Any help with organising my files and folders would be appreciated.
> Adam
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

More information about the Mod_python mailing list