[Tutor] Re: CGI Question

Derrick 'dman' Hudson dman@dman.ddts.net
Sat, 13 Jul 2002 01:20:48 -0500


--df+09Je9rNq3P+GE
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Tue, Jul 09, 2002 at 12:17:39PM -0700, Danny Yoo wrote:
| On Tue, 9 Jul 2002, SA wrote:
|=20
| > 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:

[...]

| I'd recommend pulling this out of the inner loop altogether, and to
| relocate it to the beginning:
|=20
| ###
| print "Content-Type: text/plain\n\n"
| for pageID in QueryString.keys():
|     QValue =3D QueryString['pageID'].value
|     body =3D open(QValue, "r")
| ###

I would simplify it to the following, since this is the essence of
what that code is trying to do (and adds some error handling too) :

#!/sw/bin/python

import cgi
import cgitb
import sys

cgitb.enable(display=3D0, logdir=3D"/Users/montana/Temp")

print "Content-Type: text/plain\n\n"
QueryString =3D cgi.FieldStorage()
try :
    QValue =3D QueryString['pageID'].value
except KeyError :
    print "Error -- you didn't provide a 'pageID' argument"
    sys.exit( 0 )

try :
    body =3D open(QValue, "r")
except OSError , err :
    print "Couldn't open the file:"
    print str(err)
    sys.exit( 0 )

for line in body.readlines():
    print line


What I did, other than add some exception handling, was to remove the
loop entirely.  You didn't use the loop variable at all, you simply
looked for a single parameter.  The loop would have been useful if,
for example, you wanted to display each parameter in turn.

HTH,
-D

--=20
=20
Bugs come in through open windows. Keep Windows shut!
=20
http://dman.ddts.net/~dman/


--df+09Je9rNq3P+GE
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj0vxsAACgkQO8l8XBKTpRSWpwCgwJOTeHV40dJl6a5MzwHg0mCk
L68An3309B1jtM2UV02NoKY7BtNW1z4l
=grAv
-----END PGP SIGNATURE-----

--df+09Je9rNq3P+GE--