Writing CGI Scripts in Python

This article is neither a Python tutorial nor a CGI tutorial, but a “Python Presentation from a CGI perspective”.

The Python Reference Manual abstract describes Python as:

A simple yet powerful, interpreted programming language that bridges the gap between C and shell programming, and is thus ideally suited for “throw-away programming” and rapid prototyping. Its syntax is put together from constructs borrowed from a variety of other languages; most prominent are influences from ABC, C Modula-3 and Icon ... Python is available for various operating systems, amongst which are several flavors of Unix (including Linux), the Apple Macintosh O.S., MS-DOS, MS Windows 3.1, Windows NT, and OS/2.

It should also be noted that Python is an object-oriented language. You can write classes like in C++ or Java. I use Python every time the common guy would use Perl [“common guy”? Sheesh!—Ed, who is a die-hard Perl fan]. It has about the same functionality, while being far more readable. But it should not be restricted to a scripting language—a lot of people are using it for complete applications. It's also a perfect glue language, like Tcl, because it's easy to add new modules (written in C) to it. It can also be embedded in C applications.

Listing 1 shows my very first Python script. It's still used on a file server at the office. It deletes ~*.tmp left everywhere by buggy MS Windows applications. It's not really the common Hello World program (we'll see one later), and maybe it's not the most efficient way to do the job, but it demonstrates several features of the language:

  • Variables: Variables don't have to be declared.

  • Recursivity: See the ScanDir() function.

  • Platform independence: The os module provides constants for current directory, parent directory, and so on. See os.curdir, os.pardir...

  • for statement and lists: In Python, for works differently than in C. os.listdir() returns a list of files. For example, in Listing 1, at each iteration of:

            for p in files

p will become the value of the next element in the list. So, if files is a triplet with values:

        ['lib', 'include', 'src' ]

The first time, p will be 'lib'. At the second iteration, it will be 'include', and so on, until it has gone through the whole list.

  • Arrays and indices: An array can be referred with one or two indices. One index is used to get a single element from the array (like in C). Two indices can be used to get a subset of the array. The first index gives the from element, while the second one gives the to element. For example, if a is an array containing the values 'abcdef', a[ 2 : 4 ] will return 'cd'. There are defaults for both indices: they default to from the start and to to the end respectively. Examples: a[ 2 : ] will return 'cdef'. Negative indices can be used to count from the end; a[ -2 ] will return 'e'. See the Python Tutorial at the Python web site (http://www.Python.org/) to learn more about arrays.

  • Blocks: Unlike C or Pascal-derived languages, there are no Start-Block or End-Block separators. Python works only with indentations (and the “:” character).

One of my colleagues in the firmware department recently had some problems debugging a TCP/IP application he is writing. There is a server application running in an embedded system, and a client application running on a PC. He was stuck for two days with a protocol problem, and didn't even know if the problem came from the client or from the server. Every test version meant recompiling, eventually downloading the code in the embedded system, and so on. In addition, it's not always easy to debug a device that doesn't even have a screen—you get the point.

So, after we discussed his problems, I decided to write little Python test programs to test his applications. In less than a quarter of an hour, I had tested his server application. This included writing a Python script and running it on a console of a Linux box, concurrently to tcpdump. Since the problem didn't come from the server, I wrote another program to test his client application. This script masqueraded the server, and we immediately discovered the problem. My colleague was very impressed by the short time it took me to write those two scripts, so I gave him a copy of the Python Tutorial.

Some simple scripts using sockets can be found in Listing 2a and2b. They are from the Python Library Reference.

My company sells time and attendance software in a client/server environment. Supported platforms include Unix and NT. The biggest problem with time and attendance is that, although general functionalities are the same for all our customers, they all have special specific rules. That's why the software department is considering the inclusion of the Python interpreter in their software. It would allow on-site customization, and it is available on all our platforms.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thanks for tutorial! I found a syntax error.

Anonymous's picture

Thanks for the tutorial. :)

There is a syntax error in listing 7, in the line "if len( fields ) = 0 :". You probably see it now, it should have been "==" and not "=" - we need the comparison operator, not the assignment operator.