cgimodel: CGI Programming Made Easy with Python
The other module, cgidisp.py, is the one in which you have to modify or insert an instance to the class Dispatcher for your application using one argument, namely parDict. For example, under class Dispatcher, if you define a method like
def cmd_myHello(self,parDict): print "<H1>Hello</H1>"
then this function is immediately available to the outside world. You can call it on the command line this way:
cgimodel.py -fun myHellowith URL (GET method)
cgimodel.py?-fun=myHelloand with HTML forms as
<FORM METHOD="post" ACTION="/cgi-bin/cgimodel.py> <INPUT TYPE=hidden name=fun value=myHello> <INPUT TYPE=SUBMIT VALUE="Say Hello"> </FORM>It's that easy!
The dispatch method under the class Dispatcher is called from cgimodel.py with one argument. This argument is the name of the function to be executed. Here is the interesting part. After prefixing the function name with the “cmd_” string, the dispatch method checks to see if such a function is available with hassattr. The Dispatcher maps the command to the function and executes it. This way, you do not have to use a lookup table to keep track of available functions. The additional overhead of adding a new command to the new function is not there; you just have to write the function and call it through the command line. The functionality is already there. This kind of pattern is possible with Python, since it is a highly dynamic language.
Please note that when calling the method, we are not using the prefix cmd_ of the method. This is explained later.
The main section of the Dispatcher class contains the following:
class Dispatcher: def __init__(self): self.debug = None def dispatch(self, command,args=None): mname = 'cmd_' + command if hasattr(self, mname): method = getattr(self, mname) if not args: return method() else: return method(args) else: print "<PRE>" self.error(command)<\n> self.ShowAvailableFunc() print "</PRE>" def cmd_Hello(self,parDict): print " Hello World !" def cmd_ShowDict(self): print "<PRE></H1>Debug Info:</H1><HR>" for k,v in parDict.items(): print "%-30s : %s " %(k,v) print "</PRE>" def error(self,s): print " #<B>Error<B>: <BB>Function ( %s ) not available\n " %s return
All your parameters are available in the parDict dictionary whether they are input from URL, FORM or command line—there is no difference. You can check for their existence in this way:
if parDict['param']: print " yes ", parDict['param'] else: print " No "The None object is returned when there is no parameter, i.e., when you try to access an unspecified parameter.
The instances inside the class Dispatcher are of two types: those that are prefixed by the “cmd_” string are qualified for calling from outside; internal instances are not visible outside. For example, the error instance cannot be called from CGI, but the instances cmd_Hello and cmd_ShowDict can be called. This convention is made to differentiate between the instances that are for internal (used inside the class Dispatcher) and external (by cgimodel/cgidisp) use.
So, add a “cmd_” prefix to the instances you want to use with CGI. For example, cmd_TopPage can be called with
cgimodel.py -fun TopPage
on the command line and
cgimodel.py?-fun=TopPagewill be the corresponding URL. The -fun is mandatory. This way, you can indicate which function you want to call. Obviously, you can have as many functions as you want, and they are CGI-ready. This is the exact requirement of larger CGI projects.
A couple of functions come with the module for free. The function DisplayFile displays colorized Python source code on the Web. This one relies on the module py2html.py, available with the standard Python distribution.
cgimodel.py -fun DisplayFile -fileName cgimodel.py
cgimodel.py?-fun=DisplayFile&fileName=cgimodel.pyNote the name=value and the & to separate the name,value pairs—the traditional method of specification for CGI.
The method cmd_ShowDict shows all dictionary items in the parDict dictionary and is useful for checking whether you have supplied the correct parameters.
Getting Started with DevOps - Including New Data on IT Performance from Puppet Labs 2015 State of DevOps Report
August 27, 2015
12:00 PM CDT
DevOps represents a profound change from the way most IT departments have traditionally worked: from siloed teams and high-anxiety releases to everyone collaborating on uneventful and more frequent releases of higher-quality code. It doesn't matter how large or small an organization is, or even whether it's historically slow moving or risk averse — there are ways to adopt DevOps sanely, and get measurable results in just weeks.
Free to Linux Journal readers.Register Now!
|Secure Server Deployments in Hostile Territory, Part II||Jul 29, 2015|
|Hacking a Safe with Bash||Jul 28, 2015|
|KDE Reveals Plasma Mobile||Jul 28, 2015|
|Huge Package Overhaul for Debian and Ubuntu||Jul 23, 2015|
|diff -u: What's New in Kernel Development||Jul 22, 2015|
|Shashlik - a Tasty New Android Simulator||Jul 21, 2015|
- Secure Server Deployments in Hostile Territory, Part II
- Hacking a Safe with Bash
- KDE Reveals Plasma Mobile
- Huge Package Overhaul for Debian and Ubuntu
- Home Automation with Raspberry Pi
- The Controversy Behind Canonical's Intellectual Property Policy
- Shashlik - a Tasty New Android Simulator
- Embed Linux in Monitoring and Control Systems
- diff -u: What's New in Kernel Development
- Purism Librem 13 Review