COMP249: Week 5

Steve Cassidy and Yan Wang

Server Side Scripting

The Common Gateway Interface

CGI Programs

Location of CGI Programs

Server Processing

When a server recieves a request for a CGI resource:

Example: Target Output

<html>
  <head>
    <title>Demo static HTML page</title>
  </head>
  <body>
    <h1>Hello COMP249!</h1>
  </body>
</html>

CGI Script in Python

#!/usr/local/bin/python
print "Content-type: text/html\n\n"
print "<html>"
print "<head>"
print "<title>Demo static HTML page</title>"
print "</head>"
print "<body>"
print "<h1>Hello COMP249!</h1>"
print "</body>"
print "</html>"
      

http://platypus.ics.mq.edu.au/~cassidy/cgi-bin/demo.py

The first print statement is the Content-type line that specifies the media type of the output that is generated. This line is actually part of the HTTP response header sent back to the client.

This line is immediately followed by a blank line which must not contain any spaces or tabs (the '\n\n' bit). Remember the HTTP protocol.

CGI programs often fail since the programmer forgot the blank line.

After the blank line comes the HTML encoded text which is displayed on the user's browser.

On Platypus

CGI Script Processing

Diagram of CGI
	    process

Python Numbers

>>> 1+2

3



>>> #add comments
...

>>>7/2

3

>>>7./2

3.5

Python Strings

In Python, strings can be enclosed by single quotes or double quotes.
>>> 'a tring'
'a tring'
>>> "a string"
'a string'

>>> print 'This is', 'a test'
This is a test
Multi-line string can be written as
>>> str="line 1 \n\
line 2 \n\
line 3"
>>> print str
line 1
line 2
line 3
>>>
Alternatively, use triple-quotes """ or ''' to surround the string.
>>> str="""line 1
line 2
line 3"""
>>> print str
line 1
line 2
line 3
>>>

Python Lists

A Python list contains a set of values of compound data types.
>>> a=['hi', 10, 'hello', 200]
>>> a
['hi', 10, 'hello', 200]
>>> a[0]
'hi'
>>> a[1]
10
>>> a[:2]
['hi', 10]
>>> a[2:]
['hello', 200]
>>> a. append(300)
>>> a
['hi', 10, 'hello', 200, 300]
>>> a[4]=3
>>> a
['hi', 10, 'hello', 200, 3]
>>>a.remove(3)
>>> a
['hi', 10, 'hello', 200]
>>> 200 in a
True
>>> a.index(200)
3

Python Tuples

A Python tuple contains a set of values of compund data types.
>>> a='hi', 10, 'hello', 200
>>> a
('hi', 10, 'hello', 200)
>>> a[0]
'hi'
>>> a[1]
10
>>> a[:2]
('hi', 10)
>>> a[2:]
('hello', 200)
>>> len(a)
4
>>> 200 in a
True

>>> print "Hello %s! How do you like %s?" % ('COMP249 students', "today's topic") # %s is the placeholder

# what is the output?

Python Dictionaries

>>> dic={'hi': 101, 'hello': 200}
>>> dic
{'hi': 101, 'hello': 200}
>>> dic['hi']
101
>>> dic.keys()
['hi', 'hello']
>>> dic.has_key('hello')
Ture
>>> 'hello' in dic
True
>>> 200 in dic
False
>>> for k, v in dic.iteritems():
     print k, v
...
hi 101
hello 200


>>> for x in dic:
      print x, dic[x]
...

hi 101
hello 200

if statements

x = int(raw_input("Enter an integer: "))

if  x < 0:
print "negative"
elif x == 0:
print "zero"
else:
print "positive"

for statements

l = ['COMP125', 'COMP249', 'COMP344']
for x in l:

  print x, l.index(x)



output:

COMP125 0

COMP249 1
COMP344 2
l = ['COMP125', 'COMP249', 'COMP344']
for i in range(len(l)):

  print i,  l[i]



output:
0 COMP125
1 COMP249
2 COMP344



>>>range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>range(2:5)
[2, 3, 4]

functions

def fib1(n):
  a, b = 0, 1
  while b < n:
    print b
    a, b = b, a+b


>>>fib1(10)
1 
1
2
3
5
8


or




def fib2(n):
  result = []
  a, b = 0, 1
  while b < n:
    result.append(b)
    a, b = b, a+b
  return result

>> l = fib2(10)
>>> l
[1, 1, 2, 3, 5, 8]

Generating a Table Dynamically

#!/usr/local/bin/python

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

print """
<html>  
        <head><title>Table Demo</title></head>  
        <body>    
        <h3>COMP249 Staff</h3>    
          <table> 
       <tr><th>Lecturer</th><th>Unit</th></tr>
"""
dict = {'Yan':'COMP249','Steve':'COMP249'}
for key, value in dict.items():
    print "    <tr><td>", key, "</td><td>", value, "</td></tr>"
print """    
    </table>  
  </body>
</html>
"""

The HTML Form Code

<html>
  <head><title>A simple HTML Form Page</title><head>
  <body>
     <h3> A simple HTML Form Page</h3>
     <hr>
     <form action="cgi-bin/process.py">
       <p><strong>Enter your name:</strong></p>
       <p><input type=text name=user></p>
       <p><input type=submit></p>
     </form>
  <body>
<html>

The Query String Format

How is the Query String transmitted?

GET vs. POST

GET encodes the query string in the URL

GET vs. POST

A Simple CGI Script

#!/usr/local/bin/python

import cgi                            # imports cgi module
import cgitb; cgitb.enable()          # traceback manager, displays
                                      # errors in the Web browser

form = cgi.FieldStorage()             # retrieves form input
print "Content_type: text/html\n\n"   # output MIME type

html = """
<html>
  <head><title>Greetings</title></head>
  <body>
    <h3>Greetings</h3>
    <p>%s</p>    
  </body>
</html>
"""
      

A Simple CGI Script

if not form.has_key('user'):
    print html % "Who are you?" 
else:
    print html % ("Hello, %s." % form.getvalue('user'))

Examples: client_py.html process.py

 

Environment Variables

Environment Variables in Python

In Python, HTTP environment variables are available (when set) via the os.environ dictionary.

#!/usr/local/bin/python

import os

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

if os.environ.has_key('SERVER_PORT'):
    server_port = os.environ['SERVER_PORT']
    print '<p> SERVER_PORT:', server_port, '</p>'
else:
    print '<p> SERVER_PORT: unknown </p>'

Query String

The QUERY_STRING variable always appears in the os.environ dictionary, even though its value is '' (the empty string).

#!/usr/local/bin/python

import os

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

if os.environ.has_key('QUERY_STRING') and  \               
           os.environ['QUERY_STRING'] != '':
     query_string = os.environ['QUERY_STRING']
     print '<h3> QUERY_STRING:', query_string, '</h3>'
else:
     print '<h3> QUERY_STRING: unknown </h3>'

Examples: name=Steve, name=Steve&age=21

Debugging a Script

#!/usr/local/bin/python

import cgi
import cgitb

cgitb.enable()
# cgi.enable(display=0, logdir="/temp")

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

print "<h3>Hello</h3>"
print xxx                 # Here is a name error

Example...

refer to http://epydoc.sourceforge.net/stdlib/cgitb-module.html