CGI Help Please.

SA sarmstrong13 at mac.com
Tue Jul 9 15:08:46 EDT 2002


Ok. I 'hacked' at the code for awhile and have finally figured it out. For
anyone that wants to see the results, check my test program below:

On 7/9/02 11:51 AM, "SA" <sarmstrong13 at mac.com> wrote:

#!/sw/bin/python

import cgi 
import cgitb

cgitb.enable(display=0, logdir="/Users/montana/Temp")

QueryString = cgi.FieldStorage()
for pageID in QueryString.keys():
    QValue = QueryString['pageID'].value
    body = open(QValue, "r")
    for line in body.readlines():
        print "Content-Type: text/plain\n\n"
        print line
    body.close()
    
print "Finished!"

The line in the testtext file is of course "Hello World!" So clicking the
Test link on the first webpage sends the querystring (filename of the text
file) to the python cgi script, and the script translates the data and
displays the following on a new web page:

Hello World!
Finished!

So everything is groovy now and I can start developing my own website.
Thank You all for your help.

Thanks.
SA


-- 
"I can do everything on my Mac I used to on my PC. Plus a lot more ..."
-Me


On 7/9/02 11:21 AM, in article B95077C0.924E%sarmstrong13 at mac.com, "SA"
<sarmstrong13 at mac.com> wrote:

> I've been working on a little test script so I can understand the
> functionality of the cgi module when writing a python cgi script. The goal
> is to redirect a page from a link o the stored value in a query string.
> (ex.<a href="http://localhost/cgi-bin/test2?pageID=testtext"
> title="TestFile">TestFile</a>).
> 
> I've now managed to get my script to work part way. I got rid of the server
> error I was getting by changing:
> #!/usr/bin/env python
> 
> To
> 
> #!/sw/bin/python
> 
> sw/bin is the directory where my default python is located. For some reason
> my Apache setup chokes on the /usr/bin/env executable.
> 
> So this fixes the server error. I then set the program to display the value
> for the key "pageID", but what I get is:
> 
> The test file is:
> 
> ['testtxt']
> Finished!
> 
> 
> What I would like to do now is take that dictionary value(testtxt), which is
> the name of the text file I would like to use, read the text in the text
> file and re-display this in a new web page. Basically what I have is a html
> template file and a lot of text files. My final script will take the query
> string from the href as a text file name and send that to the python cgi
> script. That value will then be used to look up the textfile, read it's
> data, and print the file in the body section between <pre></pre> tags on a
> new web page. With this script, I will have only a template file that needs
> it's links updated when I add new textfiles instead off 100+ html files
> needing to be constantly maintained.
> 
> So since I'm new to python and CGI, I've been 'hacking' around on my machine
> with different codes trying to understand how the script would function. Can
> anyone help me please?
> 
> Below is the test script I wrote:
> #!/sw/bin/python
> 
> import cgi
> import re
> 
> QueryString = cgi.parse_qs('pageID=testtxt')
> 
> for pageID in QueryString.keys():
>   QValue = QueryString[ pageID]
> #    passvalue = [Qvalue]
> #    body = open("QValue", "r")
> #    for line in body.readlines():
>   print "Content-Type: text/plain\n\n"
> #        print line
>   
>   print "The test file is:\n"
>   print QValue
> #    body.close()
>   
> print "Finished!"
> 
> Any ideas on what I'm doing wrong?
> 
> Thanks.
> SA
> 




More information about the Python-list mailing list