Source code for: 'tutor4.py'
#!/usr/bin/python
"""
runs on the server, reads form input, prints HTML;
URL http://server-name/cgi-bin/tutor4.py
"""
import cgi, sys
sys.stderr = sys.stdout # errors to browser
form = cgi.FieldStorage() # parse form data
print('Content-type: text/html\n') # plus blank line
# class dummy:
# def __init__(self, s): self.value = s
# form = {'user': dummy('bob'), 'age':dummy('10')}
html = """
<TITLE>tutor4.py</TITLE>
<body text="#000000" leftmargin=0 topmargin=0 bgcolor="#C2C2C6">
<table width=850 border=0 cellspacing=0 cellpadding=0>
<tr>
<td align=center>
<H1>Greetings</H1>
<HR>
<table width=500>
<tr><td><H4>%s</H4>
<tr><td><H4>%s</H4>
<tr><td><H4>%s</H4>
</table>
<HR>
"""
if not 'user' in form:
line1 = 'Who are you?'
else:
line1 = 'Hello, %s.' % form['user'].value
line2 = "You're talking to a %s server." % sys.platform
line3 = ""
if 'age' in form:
try:
line3 = "Your age squared is %d!" % (int(form['age'].value) ** 2)
except:
line3 = "Sorry, I can't compute %s ** 2." % form['age'].value
print(html % (line1, line2, line3))
print("""
</table></td></tr></table>
""")