A Web-Based Lunch Ordering System
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.
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 4 5 print "Content-Type: text/plain\n\n" # necessary for the browser 6 7 lunchForm = cgi.FieldStorage() # retrieve the values 8 9 for name in lunchForm.keys(): 10 print "Key= " + name + " Value= " + lunchForm[name].value + " " 11 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.
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.
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:
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.
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 <email@example.com> Subject: loi loi Sender: nobody@localhost SourceIP 220.127.116.11 firstname.lastname@example.org 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.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Chris Birchall's Re-Engineering Legacy Software (Manning Publications)
- The Italian Army Switches to LibreOffice
- Linux Mint 18
- Petros Koutoupis' RapidDisk
- ServersCheck's Thermal Imaging Camera Sensor
- Oracle vs. Google: Round 2
- The FBI and the Mozilla Foundation Lock Horns over Known Security Hole
- Privacy and the New Math
Until recently, IBM’s Power Platform was looked upon as being the system that hosted IBM’s flavor of UNIX and proprietary operating system called IBM i. These servers often are found in medium-size businesses running ERP, CRM and financials for on-premise customers. By enabling the Power platform to run the Linux OS, IBM now has positioned Power to be the platform of choice for those already running Linux that are facing scalability issues, especially customers looking at analytics, big data or cloud computing.
￼Running Linux on IBM’s Power hardware offers some obvious benefits, including improved processing speed and memory bandwidth, inherent security, and simpler deployment and management. But if you look beyond the impressive architecture, you’ll also find an open ecosystem that has given rise to a strong, innovative community, as well as an inventory of system and network management applications that really help leverage the benefits offered by running Linux on Power.Get the Guide