A Web-Based Lunch Ordering System

The author demonstrates how easy it is to write in Python—and make sure you get steamed, not fried rice.
Viewing lunch.html Via the Web Server

I then placed the lunch.html file in the directory /home/httpd/html:

$ cp lunch.html /home/httpd/html

(This is the default location Apache looks for html files. It can be configured to look elsewhere.) Once you have done this, you can see what lunch.html looks like by browsing http://localhost/lunch.html using Netscape (or any browser).

Write the CGI script with Python, which is distributed with the Red Hat Linux distribution (see http://www.python.org/). After consulting the Python documentation, which also came with the system, my first script looked like the one shown in Listing 3. It is essentially a cannibalised version of an example found in the Python documentation. To make use of this script, you'll need to point the CGI script specified in the ACTION statement in the HTML file to this script instead. That is, change the cgi script specified in the ACTION statement from lunch.cgi to first.cgi.

Testing the Installation

I then copied first.cgi to the directory /home/httpd/cgi-bin:

$ cp first.cgi /home/httpd/cgi-bin

Essentially, I interrogated all the variables sent to the script by the form and printed it back out. All output printed will be displayed by the browser.

 1 #!/usr/bin/env python
 2 # first.cgi
 3 import cgi                                   # import the cgi module
 5 print "Content-Type: text/plain\n\n"       # necessary for the
 7 lunchForm = cgi.FieldStorage()               # retrieve the values
 9 for name in lunchForm.keys():
10     print "Key= " + name + " Value= " +
lunchForm[name].value + " "
12 print "bye."
When the Go button on the lunch.html page is clicked and the first.cgi script is activated, the output returned to the web browser looks like that shown in Figure 2.

Figure 2. Output of Script first.cgi

You will notice that the keys found in the CGI script correspond to the variables I used in lunch.html.

Once I got this simple script working, I then expanded it to do what I wanted (see Listing 3). The Python code is quite straightforward and self-explanatory. It imports the CGI module, then calls the member function FieldStorage() of CGI. Whether the information is sent using the GET or POST method is hidden from you. That's how all the information sent by the web page is retrieved. The information can then be extracted by accessing lunchForm.

Listing 2

The body of the mail sent is then constructed via a series of writes to sendmail, a UNIX sendmail mail transfer agent. I decided to mail the lunch order to user lunch@localhost. An alias can be inserted in file /etc/aliases:

lunch:          chai@localhost

where user chai@localhost is organising the orders. This way, if the lunch organiser gets changed, the file /etc/aliases needs to be changed and not the CGI script. (newaliases needs to be run for changes to /etc/aliases to take effect).

Easy, eh? Well, it could be much worse.

Hooking Everything Together

I then copied lunch.cgi to the directory /home/httpd/cgi-bin:

$ cp lunch.cgi /home/httpd/cgi-bin

I opened Netscape, typed in http://localhost/lunch.html as the URL, filled in the form, selected my order and clicked on “Go”.

Sometime later, an e-mail arrived outlining the order.

Here is what the received e-mail of the lunch order, sent by the CGI script, looks like:

>From nobody@localhost  Wed Apr 26 11:01:50 2000<\n>
Delivered-To: ccang@localhost
Date: Wed, 26 Apr 2000 11:01:48 +1000
To: lunch@localhost
From: chai <calcium@altavista.net>
Subject: loi loi
Sender: nobody@localhost
Wed Apr 26 11:01:01 GMT+1000 (EST) 2000
chai wants
1. L39 with Steamed rice.
2. NONE with NA rice.
3. NONE with NA rice.
4. NONE with NA rice.
Special Instructions: Make It Extra Yummy Please!

Figure 3 shows what the web page looks like after the request has been sent.

Figure 3. Snapshot of Resulting Web Page After Lunch Order Made



Comment viewing options

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

can you show us a web-based

Anonymous's picture

can you show us a web-based gift shop system?

One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix