#----- Example Of Working With Python cgi
Module -----#
import cgi
CODE
# hold the form data
username
):
username = form["username"]
usernames = ""
if type(username) is type(
):
# Multiple username fields specified
for item in username:
if usernames:
# Next item insert comma
usernames = usernames + "," + item.value
else:
# First item don't insert comma
usernames = item.value
else:
# Single username field specified
usernames = username.value
<ul>\n
for field in form.keys():
field_list = field_list + <li>%s</li>\n
% field
</ul>\n
Content-type: text/html\n\n
Content-type: image/jpeg\n\n
<html><head>
print <title>My Page</title>
print </head><body>
print <h1>Powers of two</h1>\n<ol>
for n in range(1,11):
print <li>'+str(2**n)+'</li>
</ol></body></html>
<dd>%2.3f</dd> <dt>Square-root of 2</dt> <dd>%2.3f</dd></dl>
"pi": 3.1415, "e": 2.7182, "sqrt3": 1.73205, "sqrt2": 1.4142} |
<dd>%(pi)2.3f</dd> <dt>Square-root of 2</dt> <dd>%(sqrt2)2.3f</dd></dl>
import cgi # ...do the actual work of the CGI... # perhaps ending with: print template % script_dictionary
import my_cgi # main CGI functionality in my_cgi.py
my_cgi.main()
import time errtime =- '+ time.ctime(time.time()) +
---\n' errlog = open(cgi_errlog
,a
) errlog.write(errtime) traceback.print_exc(None, errlog) print "<html><head><title>CGI Error Encountered!</title></head>" print "<body><p>Sorry, a problem was encountered running MyCGI</p>" print "<p>Please check the error log on the server for details</p>" print "</body></html>"