Fall Semester 2009


What we know at the start of Week Three
Summary of what we know so far:

a) we know what it takes to write a program in

     Perl                       Python 
1. log into silo

2. go into a folder (create one if necessary)

3. create a file (using pico -- no extension needed)

3.1 the first line in the file reads
    #!/usr/bin/perl            #!/usr/bin/python 
3.2 the rest of the file contains the program in
    perl                       python 
4. make the file executable with chmod 700 or chmod u+x

5. run the file with ./name (where name is the program's name)

b) we also know a bit of
    Perl                       Python 
Equivalent lines (lines with the same effect):
    print "howdy\n";           print "howdy"

    print "howdy";             print "howdy", 

    print "Your age: "; 
    $age = <STDIN>;            age = raw_input("Your age: ")
If "age" is a variable whose value has been read as above:
    print $age + 1;            print int(age) + 1

    print $age;                print age 

    chop($age);
    print $age;                print age,

c) web scripts need to respect the HTTP format in their output; thus
  • they need to print the headers first,
  • followed by an empty line
  • and then by the body of what they have to say.

This can be summarized by saying that

  • the following string needs to be printed at the beginning:
       "Content-type: text/html\n\n"
With this header the program promises to print HTML (as the browser typically expects).

d) any CGI script is being passed input data in a format like this:

http://silo.cs.indiana.edu:8346/cgi-bin/whatever?day=monday
Here one param
  • with the name of "day"
  • and value "monday"
is being passed to the script
  • "/cgi-bin/whatever" (written in perl or python)
http://silo.cs.indiana.edu:8346/cgi-bin/one?age=9&name=adrian
Two params ("age" and "name") are being passed to script one.

The age value is "9" (a string) and the name is "adrian" (also string).

e) finally, a web script can use a specialized library to read the incoming data:

   use CGI;                      import cgi

   $q = new CGI;                 q = cgi.FieldStorage()

   $age = $q->param("age");      if q.has_key("age"): 
                                   age = q["age"].value
                                 else: 
                                   age = ""
With this we wrote a program that acts like this:

Welcome to the program.
What's your name: Adrian
How old are you: 9
Well Adrian next year you will be 10 years old
That is, the program collects name and age then calculates and prints a report.

Here's the script written in Python:

#!/usr/bin/python

import cgi

tomato = cgi.FieldStorage()

if tomato.has_key("who"): who = tomato["who"].value
if tomato.has_key("age"): age = tomato["age"].value

print "Content-type: text/html\n\nMy Script";

print "Well", who, "you will be", int(age) + 1, "years old next year."

print "";
f) any script can use an HTML form to have the data collected and sent its way:
<html>
  <head>
    <title>My first HTML form</title>
  </head>
  <body>
    <form action=http://silo.cs.indiana.edu:8346/cgi-bin/09101300/one>
      <table border>
        <tr> <td> Name: <td> <input type=text name=name size=8/>
        <tr> <td> Age:  <td> <input type=text name=age size=3>
        <tr> <td colspan=2> Push <input type=submit value=Proceed> to send data.
      </table>
    </form>
  </body>
</html>
This is HTML document tomato.html collecting "name" and "age" for "whatever".

Challenge One. Implement a script that

  • first asks for the name,
  • then collects it and uses it to
  • ask for the age,
  • then collects the age and
  • finally prints the final report.

Here's a model for it (not much help, I am sure).

-bash-3.2$ ./three
Welcome to the program.
What's your name: Larry Bird
Well Larry Bird how old are you now: 17
Well Larry Bird next year you will be 18 years old

-bash-3.2$ cat three
#!/usr/bin/python

print "Welcome to the program." 

name = raw_input("What's your name: ")

age = raw_input("Well " + name + " how old are you now: ")

print "Well", name, "next year you will be", int(age) + 1, "years old"

-bash-3.2$ 
So we need to do this, but on the web. Write a web script with this behaviour.


Updated by Adrian German for A202/A598, A290/A590 and A348/A548