POST from a CGI

David M. Cooke cookedm+news at
Wed Sep 22 16:17:30 EDT 2004

fuzzyman at (Michael Foord) writes:

> I'm receiving POST data to a CGI, which I'd like to forward to another
> CGI using urllib2.
> I have two options -
> 1) Parse the data using cgi.FieldStorage() and then rebuild the POST
> request into a dictionary - including any files (? uploading files by
> urllib2 untested and undocumented - examples seem to be for httplib).
> 2) Read the whole POST data in using, rebuild the
> 'Content-type' and 'Content-length' headers and make the POST.
> Obviously (2) is a *lot* less fiddly and less error prone. *But* I'm
> getting 400 errors when I try it (server thinks request is malformed).
> I've checked the headers and the body data and they seem normal and I
> can't work it out.
> I wonder if anyone can see what I'm doing wrong......
> (Simple code shown first - then a straightforward example that ought
> to work and fails).
> info =         # info about the url
> pagetype = info.gettype()
> print 'Content-type: ' + pagetype + '\n'
> print          # print the received page

I think this is your problem: print is adding an extra \n to the end,
so the length of the data doesn't agree with the Content-length
header. Use sys.stdout.write.

|David M. Cooke

More information about the Python-list mailing list