Lab code examples from Week 8, should be runnable on Bluehost ~/public_html/week8-lab.html Week 8 Lab

Clicking "Post stuff!" will post the data from the text box to the server and append it to a file called data.txt. The script will then return the entire contents of that file, and this page will display it in the fileContents pre.







~/public_html/cgi-bin/week8-lab.py

#!/usr/bin/python

# Remember to make this executable: chmod a+x week8-lab.py

import cgi

# to facilitate debugging
import cgitb
cgitb.enable()

form = cgi.FieldStorage()
stuff = form['stuff'].value

# open a file and append to it ('a' mode)
f = open('data.txt', 'a')
f.write(stuff)
f.write('\n') # end it with a newline for niceness
f.close()


# now open that same file, read ALL of the contents into a string, and
# print it into the HTTP request to return to the user's browser
all_contents = open('data.txt').read()

print "Content-type: text/html"
# don't forget the extra newline!
print
print all_contents