BaseHTTPServer weirdness

Steve Holden steve at
Mon Sep 11 23:29:20 CEST 2006

Ron Garret wrote:
> In article <mailman.287.1158003082.5279.python-list at>,
>  Steve Holden <steve at> wrote:
>>The normal way is
>>s = cgi.parse()
>>since the CGI script sees the client network socket (after consumption 
>>of HTTP headers) as its standard input.
> Doesn't work.  (I even tried sys.stdin=r.rfile; s=cgi.parse())  Don't 
> forget, this is not a CGI script, it's a handler for a BaseHTTPServer.
Right. My bad. However there's clearly something screwy going on, 
because otherwise you'd expect to see at least an empty dictionary in 
the output.

>>>2.  Despite the fact that I'm passing a 1 for the keep_blank_values 
>>>argument to cgi.parse_qs, it doesn't actually keep blank values.  Is 
>>>this a bug, or am I doing something wrong?
>>Sounds like a bug, but then since your parsing looks buggy I'm surprised 
>>you get anything at all. Try using a keyword argument 
>>keep_blank_values=1 just in case the order has changed or something 
>>daft. But fix your parsing first.
Reading the source of the 2.4.3 library shows that someone added an 
environ=os.environ argument, which will be the second argument on a 
positional call, so that clears that mystery up. The doicumentation 
should really show these as keyword arguments rather than implying they 
are positionals. It'd be nice if you could report this as a 
documentation bug - though I believe by now the 2.5rc2 release will be 

>>The other thing to note is that since you are putting a dictionary's 
>>string representation out straight into your HTML if there are odd 
>>characters in it this may give you strange output in the browser, so you 
>>should view the page source to ensure that's not the case. Which it 
>>probably isn't ...
> I know that's not a problem because it does work when I use parse_qs.  
> (I know about escaping HTML and all that, but this is just a little test 
> program.)
I suspect that the remainder of your problems (cgi_parse appears to be 
returning a *string*, dammit) are due to the fact that the process you 
are running the HTTP server in doesn't have the environment variables 
set that a server would set if it really were being called in a CGI 
context, and which the CGI library expects to be set. You could try 
passing them as an explicit environ argument and see if that worked.

But basically, you aren't providing a CGI environment, and that's why 
cgi.parse() isn't working.

Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd
Skype: holdenweb
Recent Ramblings

More information about the Python-list mailing list