3.1 Introduction to CGI

#
3.1 Introduction to CGI

This section presents a very simple program which will introduce you to CGI programming. This serves two purposes; it will verify that your web server is configured to run CGI programs, and it will demonstrate how simple CGI programming can be.

The sample program from this section is supplied in the `samples/templates/simple1` directory and can be installed in your web server `cgi-bin` directory by running the following commands.

    cd samples/templates/simple1
    python install.py

The `simple.py` program is show below.

    #!/usr/bin/python

    print 'Content-Type: text/html'
    print
    print '<html>'
    print ' <head>'
    print '  <title>My CGI application</title>'
    print ' </head>'
    print ' <body>'
    print '  Hello from my simple CGI application!'
    print ' </body>'
    print '</html>'

You can see the program output by pointing your browser at http://www.object-craft.com.au/cgi-bin/alsamp/simple1/simple.py.

If after installing the program locally you do not see a page displaying the text ``Hello from my simple CGI application!'' then you should look in the web server error log for clues. The location of the error log is specified by the `ErrorLog` directive in the Apache configuration. On a Debian Linux system the default location is `/var/log/apache/error.log`. While developing web applications you will become intimately familiar with this file.

Knowing how Apache treats a request for a document in the `cgi-bin` directory is the key to understanding how CGI programs work. Instead of sending the document text back to the browser Apache executes the ``document'' and sends the ``document'' output back to the browser. This simple mechanism allows you to write programs which generate HTML dynamically.

If you view the page source from the `simple1.py` application in your browser you will note that the first two lines of output produced by the program are not present. This is because they are not part of the document. The first line is an HTTP (Hypertext Transfer Protocol) header which tells the browser that the document content is HTML. The second line is blank which signals the end of HTTP headers and the beginning of the document.

You can build quite complex programs by taking the simple approach of embedding HTML within your application code. The problem with doing this is that program development and maintenance becomes a nightmare. The essential implementation (or business level) logic is lost within a sea of presentation logic. The impact of embedding HTML in your application can be reduced somewhat by using a package called `HTMLgen`. 3.1

The other way to make complex web applications manageable is to separate the presentation layer from the application implementation via a templating system. This is also the Albatross way.

#### Footnotes

...HTMLgen. 3.1
  `HTMLgen` can be retrieved from http://starship.python.net/crew/friedrich/HTMLgen/html/main.html. On Debian Linux you can install the `htmlgen` package.