My First CGI Script
Ben Ocean
zope at thewebsons.com
Fri Apr 27 13:32:16 EDT 2001
If anyone has a simple cgi script they could share with me that does
anything remotely similar to the needs I outline herein and the script I've
cobbled together at the bottom of this email it would be appreciated!
At 10:22 PM 4/26/2001 -0600, you wrote:
>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"])
I'm lucky. This first project is dirt-down simple. I don't care what the
data is! I just need to get it to the client after the visitor enters it. I
was designing a page to put up on the site, but perhaps it would just be
easier to email the data to the client.
>There are a couple of good examples in the library reference.
I wasn't able to find anything simple. The examples I found were too
complex for newbies. Probably didn't look in the right place...
> > **How do I write a Web page on the fly and assign it the name *Report*?
>
>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.
Do I define a function called main?
TIA,
BenO
Here's my script, such as it is right now:
#!/usr/bin/python
import sys # just for debugging purposes
import cgi
import time
import string
import os
form = cgi.FieldStorage()
print 'Content-type: text/html\n\n'
# specify the filename of the page to send the dealership
Report = repr(time.time()) + ".html"
# read the entire file as a string
def main():
start = "<html><body>\nHere's the data a potential customer requested
from the Kelley Blue Book link on your Web site:<br> <br>\n"
FormData = open("/apache/vhosts/bladechevy/cgi-bin/kelleyblue.html","r")
formstuff = ""
while FormData.readline():
safe = string.maketrans("`|~!@#$%^&*()_+=:;{}[]",
"----------------------")
line =
string.replace(string.replace(string.replace(string.translate(FormData.readline(),
safe), "<input type-\"hidden\" name-\"", "<tr><td>"), "\" value-\"",
"</td><td>"), "\">", "</td></tr>\n")
formstuff = string.join([formstuff,line])
end = "</body></html>\n"
all = string.join([start,formstuff,end])
FormData.close() # close the file
if (__name__ == "__main__"):
main()
More information about the Python-list
mailing list