0

A python script of mine is successfully invoked from CGIHTTPServer. How may we access the request parameters within that script?

I have done a number of rounds of googling on this topic. I am not in a position to do mod-cgi on an Apache server for example. Apache is not running on the box - nor will it be. it is not my box and that is too big of a change to make.

If the answer is "there is no ready-made solution to accessing request parameters in a (python actually..) cgi script" - then is there another option for built-in python web server that can do cgi (with request parameters) ? Along the lines of java servlets ..

1 Answer 1

2

Within the cgi script I added (temporarily) the following code to find the environment variables:

import os
v = [i for i in os.environ.iteritems()]
pl('env is %s' %(repr(v)))

Within the rather copious output is included the answer being sought:

('QUERY_STRING', 'cpus=128&cpumillis=60') 

Then the query parameters are easily obtained as:

qs={k:v for k,v in [l.split('=') for l in os.environ['QUERY_STRING'].split('&')]}
Sign up to request clarification or add additional context in comments.

2 Comments

This is the correct answer, but you'll still have to unescape k and v. Have a look at the cgi module and urllib.urlparse.parse_qs, too.
@Phillip Thanks! I know zilcho about python web serving - beyond that there are a bunch of heavier frameowrks django node.js etc etc. We needed something small but that also supports cgi. For the solution - I will add your urllib stuff. BTW if you make it an answer I will also upvote it - though probably accept mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.