Returning HTTP POST body from CGI module

Michal Wallace sabren at manifestation.com
Sun Nov 25 05:24:24 EST 2001


On 24 Nov 2001, Stephen wrote:

> I have a Python CGI script receiving XML data via HTTP POST from 
> another company.  Unfortunately, that company doesn't seem to
> have implemented the HTTP protocol correctly.

This is completely valid HTTP. They probably ought to have 
Content-Type header in there, but it's no big deal.

> If I understand correctly, the body of a HTTP POST should be 
> URL-encoded, right ?  Since they are sending XML data in the 
> HTTP body, and it is not URL-encoded, it's not really 
> compatible with the CGI module which will be looking for
> variable/value pairs.  Is it possible to access the raw 
> HTTP header and body from the CGI module instead ? 
> Or will I really have to write a socket handler to deal with
> this ?


CGI request content comes in on stdin. The cgi lib just
parses it. The headers tend to show up in the environment,
but I'm not sure any old header you send shows up
there... The following program should give you access to the
data though:


#!/usr/bin/python
import sys, os
length = os.environ["CONTENT_LENGTH"]
print sys.stdin.read(length)
                


Cheers,

- Michal   http://www.sabren.net/   sabren at manifestation.com 
------------------------------------------------------------
Give your ideas the perfect home: http://www.cornerhost.com/
 cvs - weblogs - php - linux shell - perl/python/cgi - java
------------------------------------------------------------





More information about the Python-list mailing list