How to read POSTed data

Dan Perl danperl at rogers.com
Sun Feb 6 18:47:46 EST 2005


"Pierre Quentel" <quentel.pierre at wanadoo.fr> wrote in message 
news:42067a0d$0$10490$8fcfb975 at news.wanadoo.fr...
>
>> Pierre, I am repeating some questions I already stated in another thread, 
>> 'CGI POST problem', but do you have any opinions on how CGIHTTPServer's 
>> do_POST handles requests?  It looks to me like it always expects form 
>> data to be part of the POST command header, in the path of the URL, just 
>> like a GET request.  Am I understanding the code incorrectly?
>
> The code in CGIHTTPServer is not very easy to understand, but it does
> read the request body, as many bytes as indicated in the Content-Length
> header. See line 262 (in the Python 2.4 distribution) or 250 in Python 2.3 
> (this is the Windows version) :
>
>                 data = self.rfile.read(nbytes)
>
> Then this data is sent to the standard input of the CGI script. If this 
> script is a Python program using the cgi module, it usually creates a 
> cgi.FieldStorage() instance : upon creation, the standard input is read 
> (in self.read_urlencoded() for instance) and the string collected is 
> processed to produce a dictionary-like object, with keys matching the form 
> field names
>
> This is compliant with the CGI specification (HTTP doesn't say anything 
> about the management of data sent by POST requests). The code I sent is an 
> alternative to CGI, leaving the management of this data (available in 
> self.body) to a method of the RequestHandler instance

Thanks, Pierre, this got me much further but I hit another stumbling block. 
I can see now that CGIHTTPServer writes all the header lines into os.environ 
and creates a subprocess for the script with os.popen2 or os.popen3 (it's 
Windows), passing the form data to the new process through sys.stdin (I 
believe it is sys.stdin although the Library Reference descriptions of 
popen2 and popen3 say that would be sys.stdout).  But I don't see any of 
those headers updated in the os.environ of the cgi script's process.  Is the 
parent's os.environ passed to the subprocesses created with popen2/popen3 on 
Windows?

cgi.FieldStorage.read_urlencoded needs the content-length that should be 
passed through os.environ.

Dan 





More information about the Python-list mailing list