CGI.FieldStorage to HTML

Michael Foord fuzzyman at
Mon Sep 13 14:53:18 CEST 2004

"Graeme Matthew" <graeme.matthew at> wrote in message news:<41454367$0$22811$5a62ac22 at>...
> Hi all
> I do a CGI post, I am using CGI.FieldStorage(), I obtain the values etc al 
> works fine.
> I now need to take the same posted data and resubmit it to a remote server 
> i.e I need to rebuild the entire raw html request.
> Does anyone know if you can retrieve the entire request string before 
> parsing it via the cgi module? and then resubmit this to another server?
> Cheers
> Graeme

Before I waffle - os.environ['QUERY_STRING'] is probably the answer
you want !!

I've been implementing a cgi proxy recently which does exactly this.
Unfortunately I haven't been able to find a way to get the entire http
request and just proxy that because the server 'parses' it puts a lot
of it into the environment. (For example any authentication headers
it's impossible to retrieve from the CGI environment).

I'm doing just what you said though and rebuilding the request from
the FieldStorage and environment variables. At the moment I'm only
proxying the User-Agent headers, although I might do a few more later
though (HTTP_REFFERER etc). You can see where I've got to with

Doing 'GET' method requests is *easy*. Rebuilding 'POST' method
requests is a bit more complicated. The code I have so far will do all
requests except file uploads (I'm working on at the moment - will be
ready soon) and I haven't tested it with list values (might work...
might not). I've also just got my head around authentication (error
401 BASIC authentication and am half way through writing the code to
implement it).

Something you might find useful is my CGI. This shows you
all the environment variables etc when you fetch a url - useful for
working out what information you have available to you. (See I *think*
- which also has a demo online).

The following chunk of code shows you which environemnt variable to
use to get the query string from 'GET' type requests.

        if os.environ.get('REQUEST_METHOD','').lower()=='post':       
 # do something with post data
            thedata = fullparse(form)
            from urllib import urlencode
            txdata = urlencode(thedata)                         #
straightforward encode - need to test for/allow multipart form data
(file upload) and list types
        elif os.environ.get('QUERY_STRING', '') and not
            theurl = theurl + '?' + os.environ['QUERY_STRING']
        txheaders = getheaders(txheaders)              # proxy as many
of the headers as we can

don't worry about most of the code - but the following :
if os.environ.get('REQUEST_METHOD','').lower()=='get':
    querystring = os.environ.get('QUERY_STRING', '')

should do most of what you need...........
(os.environ is a dictionary containing the environment variables).



More information about the Python-list mailing list