Prev: cgi Up: cgi Top: Top

11.1.1. Example

This example assumes that you have a WWW server up and running, e.g. NCSA's httpd.

Place the following file in a convenient spot in the WWW server's directory tree. E.g., if you place it in the subdirectory test of the root directory and call it test.html, its URL will be http://yourservername/test/test.html.

Selecting this file's URL from a forms-capable browser such as Mosaic or Netscape will bring up a simple form with two text input fields and a ``submit'' button.

But wait. Before pressing ``submit'', a script that responds to the form must also be installed. The test file as shown assumes that the script is called test.py and lives in the server's cgi-bin directory. Here's the test script:

The script should be made executable (`chmod +x script'). If the Python interpreter is not located at /usr/local/bin/python but somewhere else, the first line of the script should be modified accordingly.

Now that everything is installed correctly, we can try out the form. Bring up the test form in your WWW browser, fill in a name and address in the form, and press the ``submit'' button. The script should now run and its output is sent back to your browser. This should roughly look as follows:

Test Form Output

If you didn't enter a name or address, the corresponding line will be missing (since the browser doesn't send empty form fields to the server).