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 ..

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560

1 Answers1

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('&')]}
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • This is the correct answer, but you'll still have to unescape `k` and `v`. Have a look at the [cgi module](https://docs.python.org/3.4/library/cgi.html) and [urllib.urlparse.parse_qs](https://docs.python.org/3.4/library/urllib.parse.html#urllib.parse.parse_qs), too. – Phillip Jun 11 '15 at 12:22
  • @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. – WestCoastProjects Jun 11 '15 at 16:10