My First CGI Script

Jeff Shipman shippy at nmt.edu
Fri Apr 27 00:22:37 EDT 2001


Ben Ocean wrote:
> **How do I capture the data?

What I normally do first is

form = cgi.FieldStorage()

That grabs all of the data from your form. Then, you
can grab the individual components of your form by
using it like a dictionary. (ie: form["name"], form["address"])

There are a couple of good examples in the library reference.

> **How do I write a Web page on the fly and assign it the name *Report*?

What I usually do is just have the cgi print out the webpage
to be viewed. You could always write the results to a file
and call it Report.html and do some funky redirecting, but
it's just nasty and you get into other security issues for
which you need a C wrapper with the SUID bit to allow you to
write to files in your directory (assuming you're using Linux)
and...well, this is straying from the topic of Python.

Anyway, as I was saying, what I normally do is just print the
results to stdout like so:

print 'Content-type: text/html\n\n'

print '<HTML>\n'\
      '   <BODY>\n'\
      '      This is my name: '+form["name"]+'\n'\
      '   </BODY>\n'\
      '</HTML>'

and your browser should pick it up just fine. Remember
the above is just an example though and you should probably
do error checking on the members of form you're trying
to access before actually using them.

Good luck!

-- 
Jeff "Shippy" Shipman     E-Mail: shippy at nmt.edu
Computer Science Major    ICQ: 1786493
New Mexico Institute of Mining and Technology
Homepage: http://www.nmt.edu/~shippy



More information about the Python-list mailing list