Making urllib2's POST 302 handle same as Perl LWP's behaviour

Karra skarra at gmail.com
Wed Dec 29 05:57:31 EST 2010


I am doing a POST to a webserver and get a 302 Found response
(redirect). urllib2's default behaviour is to do a GET on the new url
from the Location: URI in the 302 response.

This is different from what I have found with LWP::UserAgent-
>request() in perl. After much searching I understand there is a view
that automatic redirection for a 302 in response to a POST is not in
conformance to the relevant RFCs. Therefore, I believe urllib2's
behaviour appears to be non-conformant (as, I believe are many
browsers).

Now, regardless of what is the "correct" approach to handling the 302,
there is some information in the returned html of the 302 which I am
losing because of the subsequent GET. I tried to raise a HTTPError
from redirect_request() but that just kills the connection with the
server. I tried returning None, same result.

Can someone point me to how I can get the default LWP:UserAgent
behaviour of handling this scenario using urllib2?



More information about the Python-list mailing list