Python simple web development
Kee Nethery
kee at kagi.com
Sat Jun 27 14:35:53 EDT 2009
Until I'm an experience Python coder, I'm sticking with built-in
packages only. My simple CGI is:
#!/usr/bin/env python
# this simple CGI responds to a GET or a POST
# send anything you want to this and it will parrot it back.
# a line that starts with #2 is the old-style code you should use that
works
# with versions less than Python 2.6. I'm using 2.6.2 and am trying to
use
# code that works with Python 3.x and up.
import cgi ## so that I can be a web server CGI
import os ## only purpose is for getting the CGI client values like IP
and URL
def main():
# create the output variable and then add stuff to it that gets
returned
cgiResponseData = 'stuff from the client browser connection:\n'
# so that there is something to return, go through the CGI client
data
#2 for cgiKey in os.environ.keys():
for cgiKey in list(os.environ.keys()):
# for each client data value, add a line to the output
cgiResponseData = cgiResponseData + \
str(cgiKey) + ' = ' + os.environ[cgiKey] + '\n'
# this says give me a list of all the user inputs posted to the cgi
formPostData = cgi.FieldStorage()
cgiResponseData = cgiResponseData + '\n\nstuff from the URL POST
or GET:\n'
# cycle through those inputs and output them right back
#2 for keyValue in formPostData:
for keyValue in list(formPostData):
cgiResponseData = cgiResponseData + \
str(keyValue) + ' = ' + formPostData[keyValue].value + '\n'
#2 print 'Content-type: text/html'
#2 print
#2 print cgiResponseData
print('Content-type: text/html')
print('')
print(cgiResponseData)
main()
More information about the Python-list
mailing list