CGI.FieldStorage to HTML

G

Graeme Matthew

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
 
M

Michael Foord

Graeme Matthew said:
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
http://www.voidspace.org.uk/atlantibots/pythonutils.html#cgiproxy

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 http_test.py 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
http://www.voidspace.org.uk/atlantibots/recipebook.html#http 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
data['id']=='mjf-approx':
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).

Regards,

Fuzzy

http://www.voidspace.org.uk/atlantibots/pythonutils.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top