BASIC CGI PROGRAMMING





BASIC CGI PROGRAMMING

The basic process of setting up your Python based CGI application is to start by sending back the HTTP header. The Python cgi module does not provide any solutions for generating this information for you. This means that you will need to translate any CGI module instructions of the form:

use CGI;

my $query = new CGI;
print $query->header('text/html');

into a Python print statement:

print 'Content-type: text/html\r\n\r\n',

To get the information supplied to fields in a form, the easiest method is to use the param() function in the Perl CGI module:

use CGI;

my $name = param('name');
my @options = param('options');

In Python this translates to using the getvalue() function:

import cgi

name = cgi.getvalue('name')
options = cgi.getvalue('options')

The getvalue() function also accepts a second argument, which is the value returned if the form doesn't have a field matching the name you've requested, you can use this in the same way as you would use the || operator in Perl. For example:

$name = param('name') || 'nobody';

can be rewritten as:

name = cgi.getvalue('name', 'nobody')

That is, unfortunately, where it ends when it comes to migrating from Perl to Python. All of the other options supported by the CGI module are unsupported in Python, either in the cgi module or any others.

Generating HTML

For generating HTML the easiest way to do this in Python is to generate the HTML manually using print statements. Although not elegant, it's the easiest solution available if you don't want to use any additional third party modules. Alternative, you can get hold of the HtmlKit module extensions (www.dekorte.com/Software/Python/HtmlKit/) these provide some basic methods for most of the HTML kit, albeit with less of a focus than that provided by the Perl CGI module.

For example, in Perl we can generate a table using:

use CGI qw/:html3/;
print table(Tr([th(['Name','Title', 'Phone'])]),
            Tr([td(['Martin','MD','01234 567890'])]));

Using the HtmlKit module we could rewrite this as:

import HtmlKit

table = HtmlKit.Table()
row = table.newRow()
row.newColumn('Name')
row.newColumn('Title')
row.newColumn('Phone')
row = table.newRow()
row.newColumn('Martin')
row.newColumn('MD')
row.newColumn('01234 567890')
print str(table)

It's not quite as neat, but HtmlKit does provide an easier and more structured way of defining some HTML elements, especially fonts and basic layout properties. Be warned that HtmlKit implies many different options in the HTML that is generated explicitly where CGI relies on implicit options. This is because the CGI module only outputs what you supplied to the various functions, as you can see here in the HTML generated by our earlier sample:

<table><tr><th>Name</th> <th>Title</th>
<th>Phone</th></tr> <tr><td>Martin</td> <td>MD</td>
<td>01234 567890</td></tr></table>

In comparison, the HtmlKit module adds options to the table, column, and cell definitions which you would otherwise have to explicitly disable:

<table border="0" cellpadding="3" cellspacing="1"
width="100%"><tr><td nowrap
bgcolor="#dddddd">Name</td><td nowrap bgcolor="#dddddd
">Title</td><td nowrap
bgcolor="#dddddd">Phone</td></tr><tr><td nowrap
bgcolor="#dddddd">Martin</td><td nowrap
bgcolor="#dddddd">MD<
/td><td nowrap bgcolor="#dddddd">01234
567890</td></tr></table>

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows