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?

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState