Second Summer 2009


Perl, Python, CGI.
1. Know the folders of your Apache installation.

Apache is installed in ~/apache.

There are five folders you need to always remember:

Working with Apache means going into these folders and editing various files.
2. Basic Apache maintenance.

Inevitably we need to resort to Unix commands for this topic.

To find out if your server is running:

ps -ef | grep username
(Note: needless to say username refers to, needs to be, that is, your actual username.)

To start and stop your server you need to use apachectl.

Start it with: ~/apache/bin/apachectl start.

You can use restart, stop, configtest on the command line as well (not just start as shown above).

Use crontab to set up automatic restart of your Apache server:

crontab -e will open a window in your default editor
crontab -l will show you the entries you have
crontab -r will remove all your entries (careful when you do that)
When you use crontab -e you should set your Apache entry to:
0 * * * * /u/username/apache/bin/apachectl/restart
We need to talk about:

Of these we might have to address the second in a more involved manner, we will see.

(If so, we will add some notes here.)

3. Posting HTML documents in your Apache.

We start by going into htdocs to write some HTML.

When we write htdocs we will always mean ~/apache/htdocs (your server's document root).

Go in there and let's create this file (call it one.html):

<html>
  <head>
    <title>This is my first HTML file.</title>
    <body>
      An HTML form is used to collect data from the user: <p> 
      <form action="http://silo.cs.indiana.edu:44063/cgi-bin/tuesday">
        Enter your name here: <input type=text name=something> <p> 
        Enter your age here: <input type=text name=age> <p> 
        Press <input type=submit name=whatever value=Proceed> to submit your data. 
      </form>       
    </body> 
  </head>
</html>
As we will explain the target is tuesday, a program we will develop below.
4. CGI programming in Python (and Perl).

Go to cgi-bin in your Apache installation folder.

Create this file (call it tuesday):

#!/usr/bin/python

import cgi

print "Content-type: text/html\n\n"

input = cgi.FieldStorage()

if input.has_key("something"):
  name = input["something"].value
else:
  name = ""

if input.has_key("age"):
  age = input["age"].value
else:
  age = ""

try:
  output = "Well, " + name + " next year you will be " + str(int(age) + 1) + " years old."
except:
  output = "Something went wrong. Maybe you didn't supply a valid age?"

print output
Make it executable (chmod 700 tuesday in that folder) then access it from the web.

First access it through the form you have just developed:

http://silo.cs.indiana.edu:44xxx/one.html
Note: instead of 44xxx please type your own port.

Enter the values in the form and press the submit button.

Pay attention to the URL and the content of the browser page upon reply.

That concludes the first part of accessing your program. Second, you can access it directly, like this:

http://silo.cs.indiana.edu:44xxx/cgi-bin/tuesday?something=Larry&age=58
We will discuss this second aspect in great detail when we discuss HTTP.

For now it's enough to notice that the name we spent has no space in it (we wrote Larry, not Larry Bird).

Here's now the Perl version of the Python program you've seen above:

#!/usr/bin/perl

use CGI;

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

$input = new CGI;

$name = $input->param('something');

$age  = $input->param('age');

$output = "Well, $name, next year you will be " . ($age + 1) . " years old.";

print $output;
Call this program afternoon and point the form in one.html to it, then test that it works like the other one did.


Updated by Adrian German for A202/A598