Lab 7: cgi programming in python

Comp 150

Goals:

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 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/cgi_demos/factskel, also generates one or two rows of the table, but you should delete those. The comments in fact.cgi should make that clear.

What you need to do is to modify the cgi program so that it prints the correct rows. A full working version can be observed at xenon.cs.luc.edu/~pld/cgi_demos/fact. The program contains:

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, and where the value of x in this case would be 6. 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. Mostly, just be aware that the form data itself is received in string format, and has to be converted to numeric format before you can use it.

Other improvements could be to print the second column in bold, or add a third column.

To run programs, we're going to use a small python-based webserver, in webserver.zip. (which includes fact.html and fact.cgi). Download and unzip into an appropriate work directory, and double-click on the webserver.bat file to start it. Note that it starts on port 8000, not the usual port 80, so web requests have to have ":8000" embedded in them at the appropriate place. Your first step should be to see that you can retrieve index.html; the full url is http://localhost:8000/index.html but localhost:8000 should also work. Be aware that the small webserver is not always very good about reporting errors accurately.

After that, try asking for localhost:8000/fact.html. Edit the corresponding fact.cgi file. (Note that there are two copies, one in the webserver folder and one in the formdemos/fact subfolder. Be sure you edit the right one!)

Email me your python file fact.cgi.