Here is a nice frame work for doing cgi. It helps you to run your cgi on the command line, so debugging is easy.
I have developed basically two modules to make cgi very easy to implement and maintain. The nice thing about it is that it works on the command line so you can debug your cgi modules easily. It guarantees (almost :-! ) that the behavior is same when you use it as cgi.
The first module is cgimodel.py the main one. You do not have to edit this. The second cgidisp.py module works as a dispatcher to cgimodel.py. You may have to edit this.
Any method under cgidisp.py blessed with 'cmd_' prefix is qualified for cgi call.
Other methods in cgidisp.py are internal, not available for cgi.
Example :
cgimodel.py -fun myFirstFunction -name1 value1 -name2 value2
where as myFirstFunctionis the function you have in the cgidisp.py module
the syntax
cgimodel.py -fun is a must and " -name value " etc., are optional.
This syntax works on the command line thus enables you to debug your program, you do not have to worry about setting your environment variables to do this.
command line call
cgimodel.py -fun myFirstFunction -name1 value1 -name2 value2
and equivalent http call would be
cgimodel.py?-fun=myFirstFunction& -name=value&-name2=value2&
You do not have to do anything with cgimodel.py module.
The second module cgidisp.py is the one you should add your functions.
Just edit the cgidisp.py and create an instance to the class Dispatcher.
For example the the class Dispatcher in cgidisp.py looks like
| class
Dispatcher:
def __init__(self): self.debug = None def
dispatch(self,
command,args=None):
def
error(self,s):
|
and you just have to add your functions like
| def
cmd_myFirstFunction(self,parDict):
try: val = parDict['name1'] except KeyError: print ' name1 not found ' else: print ' found', val |
The only restriction here is that your
instance name should be blessed with a prefix
cmd_" .
That's all !!
Now your cgi is ready to run. All the parameters that are supplied are available in the parDict dictionary.
Have fun !
Your suggestions
are most welcome.