Need to pass variables from mod_python publisher to standalone script.

Anthony Raj anthony_raj at persistent.co.in
Fri Jun 18 11:12:35 EDT 2004


There's a problem which I'm trying to solve as elegently as possible.

Its basically a setup that runs mod_python on Apache and its for a
client that needs to generate dynamic reports by pulling out records 
from Postgres.

Now once the report is generated , we mail the report in  csv format to 
the requested mailid.

We're trying to seperate the report generation / mailing part from the 
mod_python code by
passing the report parameters to an external python script and
processing & mailing the report externally from the httpd daemon .

We want to do this because some of the reports take something like 5mins 
or more and its really painful for
having to wait for the report to download etc. (believe me its about a 
couple million records in the db) .

The solution I have is something like this
1. Create the a script test.py
which can be called like so -
./test.py -b "{'username': ['motorola'], 'tmonth': ['1'], 
'metrics_select': ['0'], 'dateperiod': ['2'], 'keyword': ['tablecloth']}"
The parameter would actually be a dict that is processed by mod_python 
and converted into a string.

2. test.py has a function convert_both() which converts the string that 
is passed into a dictionary (attached below).
The code works well so far.

 >>>>>> test.py >>>>>>
#! /usr/bin/python
import sys
import getopt          
import t                                                                
     
#for argv in sys.argv : print "args = ",argv

def main(argv):
    try :
        opts , args = getopt.getopt(argv,"hg:b:d",["help","grammar="])
    except getopt.GetoptError:
        sys.exit(2)
       
    for o,a in opts :
        if o in ("-b","--both"): return a
                   
if __name__ == "__main__":
    both = main(sys.argv[1:])                          
    print "args(both) = ",both
                     
    d = t.convert_both(both)
    print 'd = ',d   
    # Other processing like db transactions & mailing csv files done later.


3. The issue is currently to be able to invoke this script from within a 
mod_python code .
./test.py -b "string from a dictionary" ( needs to be called from a 
python script to work)

Also I have my doubts if this could be solved in any better way .

~Anthony






More information about the Python-list mailing list