[Tutor] Re: CGI Question

SA sarmstrong13@mac.com
Tue, 09 Jul 2002 14:07:24 -0500


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@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

> On 7/9/02 11:44 AM, "Derrick 'dman' Hudson" <dman@dman.ddts.net> wrote:
> 
>> Is the page really blank?  Use the "view source" option in your
>> browser to find out.  Another helpful tool is 'tcpflow', so you can
>> watch the HTTP-level interaction between the browser and the server.
>> 
> Actually, I've made progress by 'hacking' away at the code until I got a
> response. What I actually get now is:
> 
> The test file is:
> 
> ['testtxt']
> Finished!
> 
> 
> What I have is about 20 text files that I would like to place on the web. I
> want to use a python cgi script to get the query string, read the value from
> that query string(which is the name of the text file), read that text file,
> and display it in a html template between the <pre></pre> tags. So I've been
> working on this little test script to see how the cgi module works on query
> strings. I'm new to cgi and python and all off the python cgi documentation
> I've found discusses "forms" I've found on perl/cgi document that gave me a
> hint on what to do, and then I started reading the module for cgi in python.
> I now have a test script that works to a certain extent( except I can not
> seem to place the dictionary value from the query string into a string valu
> so that I can open the file and read the contents). This is what I have so
> far:
> 
> #!/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 how to get the query string 'value' into a format that can be
> manipulated by open so that I can read the contents of the file and display
> them in a new web page?
> 
> Thanks.
> SA
>