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.



# Remember to make this executable: chmod a+x

import cgi

# to facilitate debugging
import cgitb

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

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

# 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 all_contents