[Tutor] Re: CGI Question
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Tue, 9 Jul 2002 12:17:39 -0700 (PDT)
On Tue, 9 Jul 2002, SA wrote:
> 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:
Hi SA,
> #!/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"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I think you can just print out the content type of the document once; the
web browser just looks at it the first time, and then assumes that the
rest of the output is of that type. If your body is longer than a line
(or if you have more than one query parameter), then you'll see a bunch of
'Content-type: text/plain' lines crisscrossing your output. This will
look pretty neat, but is probably not what you mean. *grin*
I'd recommend pulling this out of the inner loop altogether, and to
relocate it to the beginning:
###
print "Content-Type: text/plain\n\n"
for pageID in QueryString.keys():
QValue = QueryString['pageID'].value
body = open(QValue, "r")
###