1.1 Create a form that asks a user for his/her name and some
comments. Then create two CGI scripts that create a response. The
first script displays the information which the user has submitted
("Hello", name, ". These are your comments:", comments) and asks the
user whether he/she really wants to submit the information. The second
script is invoked by the first one and displays "Thank you", name,".
Your comments have been submitted:", comments.
Form:
First CGI Script:
#!/usr/bin/env python
#
######### CGI header #######################################
import cgi
print "Content-Type: text/html\n"
form = cgi.FieldStorage()
######### get form values ##################################
name = form.getvalue("name")
comments = form.getvalue("comments")
########## HTML header ######################################
print """
First CGI script
First CGI script:
"""
########## Form with hidden variables ######################
print ""
########## HTML footer ######################################
print """
"""
-----
2.1 Use a cookie instead of hidden text in the previous exercise.
The First CGI file
#!/usr/bin/env python
#
######### CGI header #######################################
import cgi
form = cgi.FieldStorage()
######### get form values ##################################
name = form.getvalue("name")
comments = form.getvalue("comments")
######### setting the Cookie ###############################
import Cookie
C = Cookie.SimpleCookie()
C["cookie_name"] = name
C["cookie_comments"] = comments
print C
print "Content-Type: text/html\n"
########## HTML header ######################################
print """
First CGI script
First CGI script:
"""
########## Form without hidden variables ######################
print ""
########## HTML footer ######################################
print """
"""
The Second CGI File (twocookie.cgi)
#!/usr/bin/env python
#
######### CGI header #######################################
import cgi
print "Content-Type: text/html\n"
form = cgi.FieldStorage()
######### read cookie ##################################
import Cookie
import os
C = Cookie.SimpleCookie()
C.load(os.environ["HTTP_COOKIE"])
name = C["cookie_name"].value
comments = C["cookie_comments"].value
########## HTML header ######################################
print """
Second CGI script
Second CGI script:
"""
print "Thank you", name+ ". Your comments have been submitted:", comments
########## HTML footer ######################################
print """
"""
---
3.1 A hit counter: create a file that contains only the number "0". Your CGI
script must open that file for reading; read the first line of the file into
an integer; increase the number by one; close the file; open the file again for
writing (not appending); write the number to the file; close the file.
#!/usr/bin/env python
#
######### CGI header #######################################
import cgi
print "Content-Type: text/html\n"
counter = open("counter.txt","r")
line = counter.readline()
counter.close()
if line == "":
number = 1
else:
number = int(line) + 1
counter = open("counter.txt","w")
counter.write(str(number))
counter.close()
########## HTML content #####################################
print """
Some Title
Some Content ...
"""
print "You are visitor number: ", number
########## HTML footer #####################################
print """
"""