[Pythonmac-SIG] reading text from HTML form to Python using CGI
Sarwat Khan
sarwat at sarwat.net
Tue Sep 9 15:56:49 EDT 2003
On Tuesday, September 9, 2003, at 08:06 AM, Proboscis Admin wrote:
> Could you please advice. Or even basic code of what must be in the CGI
> for it to read the form.
This might be off-topic for this list but I'll bite. I've recently
decided to abandon PHP in favour of Python and I'm much happier! The
only problem is optimizing your python CGI; unless you install
something like mod_python (which is pretty old, unfortunately), it's a
bit hard to get the performance of PHP. But I've gotten close. The
biggest performance boost you can get if you're using a CGI is to make
a "frozen" python binary using the tools at:
http://www.egenix.com/files/python/mxCGIPython.html
With that python with start up instantaneously.
I've included the text of a simple CGI that should teach you a bit
about using the high-level interface for the CGI module and a little
bit about python too. I'm not sure in which way you were using the CGI
module that was causing it to raise an exception; if getfirst() used as
below cannot find the key, it returns None.
The script below works with Apple's installation of Python 2.2 in
/usr/bin/python with OS 10.2 (you can replace the first line with #!
/usr/bin/python, if you really want).
#!/usr/bin/env python
""" A web page/cgi that shows a menu of web pages to redirect
to, and then calls itself as the form action to redirect to
that web page.
CGI variables:
url - if this variable is set, the client is redirected to this.
"""
import cgi
# other stuff that's useful
import sys, os
# I didn't use this, but if you're going to join lots of strings, you
should.
from StringIO import StringIO
# let's get the form values. we actually query these in the main
function below.
gForm = cgi.FieldStorage()
def redirect(url):
"Called to redirect to the given url."
print 'Location: %s' % url
print
sys.exit() # exit the cgi now to make sure nothing else gets
printed.
def print_html():
"Called to print out html so the user can choose a url to redirect
to."
htmlTemplate = """
<html>
<head>
<title>%(documentTitle)s</title>
</head>
<h1>%(documentTitle)s</h1>
<form action="%(formAction)s" method="%(formMethod)s">
<p>Select a web site to visit:
<select name="url">
<option value="http://python.org/topics/web/basic-cgi.html"
selected>
Basic CGI Programming in Python
<option value="http://python.org/topics/web/">
Web Programming in Python
<option value="http://python.org/topics/">
Python Topic Guides
<option value="http://python.org/">
python.org
%(extraURLOptions)s
</select>
<input type="submit">
</p>
</form>
</html>
"""
# a dictionary of values to fill the html template with
htmlValues = {
'documentTitle' : 'CGI Programming Example',
# in case we're not running as a CGI,
use foo.
'formAction' :
os.path.basename(os.environ.get('SCRIPT_NAME', 'foo')),
'formMethod' : 'POST', # or GET.
'extraURLOptions' : make_option_tags_for('http://sarwat.net',
'http://sarwat.net/opensource/')}
# actually start printing html
print "Content-Type: text/html"
print
print htmlTemplate % htmlValues
def make_option_tags_for(*args):
# the *args means that args is a variable argument list. You can
pass as many
# arguments as you want to this function and they'll turn into this
list.
result = ""
for url in args:
result += '<option value="%s">%s\n' % (url, url)
return result
if __name__ == '__main__':
# get the url from the form. Use getfirst, because the cgi module
lets us store lists
# in the FieldStorage object as well (the other option is getlist)
url = gForm.getfirst('url')
if not url:
print_html()
else:
redirect(url)
{sarwat khan : http://sarwat.net}
More information about the Pythonmac-SIG
mailing list