[XML-SIG] XML, python & CGI.

THOMAS PASSIN tpassin@idsonline.com
Thu, 6 Apr 2000 09:39:13 -0400


stu <scongdon@lucent.com> asked for help with cgi scripts:

> I am trying to write a cgi script in python that processes an XML doc
> against a XSL sheet to produce HTML. However I am having some problems.
>
> I have written most of the script and its fine, however to do the
> translation I use a command line (UNIX) command to invoke XT. From
> within an interactive session it works fine. I use:
>
> x = os.popen(process_cmd + " /path/XML/" + real_button1 + ".xml
> /home/scongdon/XML/" + real_button2 + ".xsl", 'w')
>
> which places the correct arguments on the command line, and I receive
> the correct HTML response.
>
> However I cannot figure out how to put this command into a cgi script so
> the output from my command line command, produces the final webpage. All
> I get is a server error. or no output at all
>

Is your difficulty mainly:
1) You don't know how to get your web server to launch Python against the
script, or
2) You can run the Python script as a cgi page OK, but the server complains
about the output contents, or
3) You can run Python but don't know how to get the form variables from a
form in the calling html page to the script, or
4) something else?

1) is different for different web browsers, and I have only done it on
WiIndows, so I can't help there.  2) probably indicates that you haven't
provided a legal header for the returned data.  There needs to be a header
containing at least one item:value pair, then there MUST be at least one
blank line, then comes your HTML.  The header can be as simple as:

print "content-type: text/plain\n"

Normally, simply printing the output works fine since cgi expects to
send/get data on stdin/stdout.

For 3), use the standard python cgi library module.  It is easy to use and
there is enough info in the documentation to show how to use it. Basically,
all you need is this:

import cgi
form=cgi.FieldStorage()
T=form["trip"].value

T now contains the value for the "trip" variable from the html form that
called the script.

BTW, I have run XT from a python script (under Windows) as you want to do
and it worked fine.  I used execv (I think it was that flavor of exec)
instead of popen.

Tom Passin