POST from a CGI

Michael Foord fuzzyman at gmail.com
Thu Sep 23 04:38:54 EDT 2004


cookedm+news at physics.mcmaster.ca (David M. Cooke) wrote in message news:<qnkr7ou2h45.fsf at arbutus.physics.mcmaster.ca>...
> fuzzyman at gmail.com (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 sys.stdin.read(), 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).
>  [snip]
> > info = u.info()         # info about the url
> > pagetype = info.gettype()
> > print 'Content-type: ' + pagetype + '\n'
> > print u.read()          # 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.

Those instructions occur *after* the 400 error. The 400 occurs when
the request is first sent to the server..... In actual fact I think
the problem is with the bleeding edge version of ClientCookie I was
using. When I tested my code with urllib2 it worked as expected !!

Thanks 

Michael Foord
http://www.voidspace.org.uk/atlanitbots/pythonutils.html



More information about the Python-list mailing list