Debug python CGI

Alex Martelli aleax at aleax.it
Sat Jan 26 06:49:30 EST 2002


Steven Feil wrote:

> Does the python CGI module have a test mode where the script can be
> executed from a command line instead of web server?  It seams strait

Yes, you can run a Python script that imports cgi from the command line,
e.g suppose the script acgi.py is:

[alex at lancelot alex]$ cat acgi.py
import cgi
 
print 'Content-type: text/html\n'
 
fs = cgi.FieldStorage()
 
for k in fs.keys():
    print '<p>[%s] -> [%s]</p>'%( 
        cgi.escape(k),
        cgi.escape(fs[k].value)
        )

You can test it out at will, e.g. on Unix:

[alex at lancelot alex]$ QUERY_STRING='x=y&z=t' python acgi.py
Content-type: text/html
 
<p>[x] -> [y]</p>
<p>[z] -> [t]</p>


or similarly on Dos with a SET QUERY_STRING= before running
python on the script.

> forward to make a cgi.Debug class as a replacement for the
> cgi.FieldStorage class. All that would be needed for a Debug class is
> an __init__() method could read the data from a text file.  the Debug

It's easier than that -- FieldStorage already reads form data from
either environment variable QUERY_STRING, or from standard input
(so if you have it in a file you can pipe it in...):

 
[alex at lancelot alex]$ echo 'x=y&z=t' | REQUEST_METHOD=POST python acgi.py
Content-type: text/html
 
<p>[x] -> [y]</p>
<p>[z] -> [t
]</p>

> Does anyone know if something like this has already been done for
> Python?

There's so little to do, even if you want to write a Python script rather
than using bash, a .BAT, and so on...


Alex




More information about the Python-list mailing list