Intro to Python: Modules and CGI

In Programming, Python by timfanelliLeave a Comment

I left off yesterday in my Python series having shown you the very basic language elements. Today, we’re going to expand on that a little bit by introducing modules and packages, as well as making a Hello, World! CGI script.

Each Python source file is a Python module. The module’s name is the name of the source file (without its extension), and the attributes, methods, and classes of that module are defined by the contents of the source file. Modules access other modules by ‘importing’ them — which I’ll show you below. To any of you with a Java background, this is extremely similar to Java’s ‘import’ statement being used to identify the other classes that a Java class uses.

Before we get to that though, there’s another very important point about Python. Everything is an object. Now I’m sure you’ve heard that before about other languages, such as Java (commonly – and incorrectly – described as a ‘purely-object oriented language’) — but it’s true this time. As such, a module is an object. When you import a module, you are actually instantiating a module object bound to a variable with the same name as the module. That module object has the attributes and methods defined in the corresponding source file; and you access them as you would attributes and methods of any first class object.

So here we go, let’s jump right into the CGI Hello World example to show you the CGI module, and then we’ll talk about the CGI module below.

#!/usr/bin/env python
import cgi

field = cgi.FieldStorage()
print "Content-Type: text/plain\n\n"
print 'Hello, world!\n'
print "You submitted the following values in your query string:"
for x in field.keys():
        print "\t" + x + " : " + field[ x ].value 

Save this file as helloworld.cgi, and save it to your web server’s cgi-bin directory, and run it… see what happens! It’s very simple, almost embarrassingly so, but it serves our purpose. It says Hello, world!’ and then prints a list of all the request parameters.

The #!/usr/bin/env python directive in the first line causes the CGI script to be interpreted by the Python environment. If we were executing the script directly, using python helloworld.cgi, we wouldn’t even need this, but it’s there for the web server, so it knows what to do with the CGI script when it executes.

The CGI module, importing in the second line, is very simple to use. As you can see, the CGI module defines the FieldStorage object (we haven’t talked about object oriented programming in Python yet, that’ll be my next post… for now, think of FieldStorage as a function that returns a dictionary… ofcourse functions are objects too, you know.) When the FieldStorage object is initialized, it parses the CGI request string, and populates itself as a dictionary of key=value pairs from it. The CGI module hides the request method (GET or POST) from you, and parses either type of request.

The print method writes out what is interpreted as the response by the web server. Easy :). So that’s all you really need to start writing dynamic html using Python and CGI.

The only other thing in the CGI module is a function called ‘escape’. It takes a string, and returns a copy of it after replacing all occurrences of ‘&’, ‘<‘, and ‘>’ with the corresponding HTML character ‘&amp;’, ‘&lt;’, and ‘&gt;’

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.