Lab 8: cgi programming in python on xenon
Comp 150, Dordal, March 24, 2006
Goals:
- Managing files on xenon
- Python programming using cgi
- Dynamic creation of html tables
Table of factorials
You can see what the end result is supposed to do at
xenon.cs.luc.edu/~pld/fact:
The first page prompts you for numbers M and N, and when you click submit
you get a table of factorials from M to N.
I've provided you with that first page, fact.html,
and a starter cgi script
fact.cgi
(click here to view) that contains
the basics for creating the output page, with table.
The starter, which you can observe in action at
xenon.cs.luc.edu/~pld/fact2,
also generates one or two rows of the table, but you should delete that.
What you need to do is to modify the cgi program so that it prints
the correct rows.
The program contains:
- main, which figures out M and N from the form and calls various printing functions.
- fact(k), which calculates the factorial
- printprolog, which prints the html as far as getting the table started
- printepilog, which prints the html from closing the table to the end
You need to provide the printrow(x,y) function that, if x and y
have values 6 and 720 respectively, will produce four lines as follows:
<tr>
<td> 6 </td>
<td> 720 </td>
</tr>
This is the HTML that generates one row of the table.
Hint: to get the second line above, use print "<td>, x, </td>",
with quotes as shown. Start with
def printrow(x,y):
as shown in the fact.cgi starter file.
You will then need to change main to call your printrow
function, in a for loop.
Do this between printprolog and printepilog; get rid of the line
I had there).
for i in range(M, ???):
printrow(i, ???)
If you want to get fancy, check that the strings Mstr and Nstr
consist of digits only before you convert them via M=int(Mstr), etc.
If there are nondigits, conversion with int() will cause an error.
Other improvements could be to print the second column in bold, or add a third column.
I will give you your username on xenon. The password is the first five letters
followed by ".2006"; eg "pdord.2006". Working on xenon.cs.luc.edu is something
of a pain; here are some pointers.
- Use the "PuTTY" program to connect to xenon.
It's in Loyola_Software => Internet_Tools.
Log in, and you have in effect a "command" window on xenon; note that it wants Unix commands
and not DOS ones.
Your first step is to create the "public_html" directory:
mkdir public_html
- Download fact.html and fact.cgi, as above, to your windows box.
- Start up FileZilla (also in Internet_Tools), connect to xenon,
enter the public_html folder on xenon,
and copy over fact.html and fact.cgi from your winbox.
You might also go to FileZilla Edit=>Settings and raise the idle-time disconnect
from 30 seconds to, say, 900.
- Using the PuTTY window, in the xenon directory public_html, type ls -l
to see your files (this is the Unix equivalent of dir).
- Using a browser, look at xenon.cs.luc.edu/~USERNAME/fact.html.
See if things work at all. Don't forget the ~.
- Use the PuTTY window to execute the xenon command dos2unix fact.cgi
- Use the PuTTY window to execute chmod +x fact.cgi
- Edit fact.cgi on the winbox. To get it to xenon, use FileZilla followed
by the dos2unix and chmod commands above.
- You can do some checking by loading fact.cgi into python on
your winbox. To do this, you must copy it to fact.py. Then,
start a command window on the winbox, go to the directory where fact.py is located,
start python, and type: from fact import *. Note that main will run.
You can run it again, and also parts like printrow, at your leisure.
Email me your python file fact.cgi, or give me an in-class demo.