Python Exercises lab 8

8.1 Prerequisites and learning outcomes

Before you start you may prefer to have completed lab 7. However as this lab is primarily to do with software installation, relatively little Python knowledge is directly required to complete it. However, you will need a working knowledge of the Python language to make progress with CGI programming beyond these simple examples.

At the end of this lab you will have:

8.2 Installation of Apache on Windows

a. A folder containing an Apache installed tree (i.e. the folder plus set of sub-folders and files etc.) is present on network drive G: in the G241 lab. Check that you have at least 2MBytes of free space on your H: drive. If you have less than this you will need to remove and/or backup unwanted files until you have this space available.

b. Using the file explorer copy and paste the Apache folder from G:\ to H:\ . You should now have a folder called H:\Apache . This should have sub-folders called H:\Apache\htdocs and H:\Apache\cgi-bin .

You will be using the htdocs folder to install static HTML files and the cgi-bin folder to install your CGI programs.

8.3 Testing the installation

a. Save the following HTML text to a file called hello.html :

<html><head><title>hello html world</title></head>
<body>Hello HTML World!</body></html>

To do this you can copy and paste the above 2 lines into Notepad. In order to stop Notepad from putting an unwanted (and invisible) .txt extension on the end of your filename save it as "hello.html" making sure you use double quotes ("") around the filename.

b. Copy this file to hello.html in your h:\apache\htdocs folder.

c. Using the file explorer double click on the Apache executable within H:\apache. This should appear as a coloured feather icon, and a process console should be displayed. Minimise this console so that the Apache process continues to run and stays visible as a button (this also has a colored feather icon) on the desktop taskbar. This procedure makes your PC into a web server.

d. Start a web browser (Netscape, Opera, Mozilla or Internet Explorer) and input the following URL into the location field:

Hello HTML World!

should now appear in your browser's main window. If it does, you machine is running an installation of Apache which serves static pages. If it isn't you will need to get the Apache configuration working correctly before continuing. The Internet address: should always provide a "loopback" address, i.e. it is used for clients accessing server programs on the local host.

8.4 Running simple CGI programs

a. To check that your Apache installation runs CGI applications, copy and paste the simplest CGI program:

# Simplest Python CGI Program
print "Content-type: text/plain\n"
print "hello CGI world!"

into Notepad and save it as a file (using the double quote trick again) called simple.cgi .

b. Copy this file into your H:\Apache\cgi-bin folder. If you enter the URL into the location field at the top of your browser: the text hello CGI world! should appear in your browser main window. If it does, your Apache installation can run CGI programs. If it doesn't make sure that the top line of your program corresponds to wherever your Python interpreter is installed and try again.

If it still doesn't work, you will need to check the configuration of your Apache server.

c. When you have got this working, copy and paste the CGI program from the notes which displays the local time on the server in the browser window. Make sure you name it with .cgi on the end of the filename and save it within the h:\Apache\cgi-bin\ folder. Reload this URL a few times and check that the time updates. Write down one of the displays obtained in your logbook.

8.5 Writing up your installation procedures

a. Make sure you write up your CGI installation/activation procedures in your Logbook, so that you can refer to your notes later. Did you have to do anything differently from the above instructions ? What other observations did you make ?

8.6 Make a Python program send an email message.

a. Find out the name (e.g. ) or address (e.g. of the outgoing SMTP mail server for your site. This is likely to be present within the settings for your email program, or your site administrator will be able to tell you this. If you are restricted to HTML mail only and do not have more direct access to an outgoing SMTP server you will be unable to interface CGI programs to email at your site.

b. Copy and paste the example mail sending Python program in the notes, patch in the address of the outgoing SMTP mail server and suitable from and to addresses. Run this program in the standalone environment, and make sure that the message was received by opening and reading it using an email address which you normally use. If you don't have an email address you can sign up for a free one on various providers sites, eg. . Email is a store and forward system, and mail gateways can take from a few seconds to a few hours to work, so to confirm whether your test worked you may need to check you email address for new messages again later.

8.7 Inspect environment variables in CGI and non CGI programs

a. Copy and paste the program in your notes section 8.8 which inspects environment variables. Run this program in standalone (non CGI) mode.

b. Modify this program so that the environment variable key/value pairs are printed in sorted order (sorted by key). Write down in your logbook what you had to do to sort the list of keys.

c. Now install this program so that it runs in the CGI environment and compare the 2 sets of environment variables, both within and outside the CGI environment variables. Copy and paste and print the outputs. Which variables were changed ? Highlight the changes on your prints and paste these into your logbook.

d. Run the program in the CGI environment with a query string after the URL, e.g:"Fred Bloggs"

e. Compare with previous outputs to compare again which environment variable(s) have changed, compared to when you ran the CGI program without a query string. Write down the value of the environment variable in your logbook. Make up a few different query strings and describe the effect of these on this environment variable in your logbook. For these changes only print the differences. Write in your logbook how you think various punctuation marks in the URL are encoded in the environment variable.