Simple CGI request and Python reply

Tim Roberts timr at probo.com
Thu Apr 9 03:13:01 EDT 2009


Greg Corradini <gregcorradini at gmail.com> wrote:
>
>I'm trying to implement something very simple without using a Python
>WebFramework and I need some advice. I want to send a comma delimited string
>from the client to a server-side Python script. My initial plan was to use a
>JavaScript function (see below) called "makerequest" that creates a
>XMLHttpRequest object and GETs the output from a Python script. I've used
>this function before to field requests  (in those cases the parameter "data"
>passed in the XMLHttpRequest.send() method is null). So I thought I could
>just pass some data in and be able to retrieve it with Python. I'm not sure
>how to do this. I've used forms with cgi/Python before. However, I don't
>want to use a form here. I want Python to handle the "data" variable being
>passed without looking for field names using .FieldStorage(). Can that be
>done?

Well, sort of, but why wouldn't you want to use the tools that are designed
to handle this?

You are sending an HTTP GET request, which is exactly the same as entering
the URL in your browser's command line.  EXACTLY the same.  That request
will be read by your web server and converted to a CGI request for your
Python program (assuming the serverPage points to a Python script).  If you
pass parameters with the URL, you will need to parse those parameters, and
the "cgi" module already knows how to do that.

You don't actually have to use FieldStorage.  You can just say this:

    import cgi
    q = cgi.parse()

which returns the parameters as a simple dictionary.

>Maybe making a request is not the quickest route to do what I want.  Ideas?

There are few other mechanisms available in Javascript to do this.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list