Re: POST from a CGI
From: David M. Cooke (cookedm+news_at_physics.mcmaster.ca)
Date: 09/22/04
- Next message: Bengt Richter: "Re: How to count lines in a text file ?"
- Previous message: djw: "Re: detecting variable types"
- In reply to: Michael Foord: "POST from a CGI"
- Next in thread: Michael Foord: "Re: POST from a CGI"
- Reply: Michael Foord: "Re: POST from a CGI"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 22 Sep 2004 16:17:30 -0400
fuzzyman@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.
-- |>|\/|< /--------------------------------------------------------------------------\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca
- Next message: Bengt Richter: "Re: How to count lines in a text file ?"
- Previous message: djw: "Re: detecting variable types"
- In reply to: Michael Foord: "POST from a CGI"
- Next in thread: Michael Foord: "Re: POST from a CGI"
- Reply: Michael Foord: "Re: POST from a CGI"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|