Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Making urllib2's POST 302 handle same as Perl LWP's behaviour

Reply
Thread Tools

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

 
 
Karra
Guest
Posts: n/a
 
      12-29-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?
 
Reply With Quote
 
 
 
 
Karra
Guest
Posts: n/a
 
      12-29-2010
On Dec 29, 3:57*pm, Karra <ska...@gmail.com> wrote:

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


Out of frustration, I decided to give 'mechanize' a try. It came as an
awesome surprise that mechanize implements the exact api of urllib2 -
meaning all I had to do was a query-replace of urllib2 to mechanize,
and lo-and-behold I got what I wanted!

Which is to say, the behaviour of mechanize matches Perl's
LWP::UserAgent as afar as handling of POST redirects go.
 
Reply With Quote
 
 
 
 
Octavian Rasnita
Guest
Posts: n/a
 
      12-29-2010
I have tried:

In httpd.conf:

Redirect /one/ http://localhost/two/
Redirect /two/ http://localhost/three/
redirect /three/ http://www.google.com/

Then I made a POST request with LWP::UserAgent to /one:

use LWP::UserAgent;
print LWP::UserAgent->new->post('http://localhost/one/')->as_string;

And the result was:
HTTP/1.1 302 Found
Connection: close
Date: Wed, 29 Dec 2010 13:58:37 GMT
Location: http://localhost/two/
Server: Apache/2.2.15 (Win32) mod_perl/2.0.4-dev Perl/v5.10.1
....

Exactly as you described.

Then I made a POST request with WWW::Mechanize:

use WWW::Mechanize;
print WWW::Mechanize->new->post('http://localhost/one/')->as_string;

And the result was:
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Connection: close
Date: Wed, 29 Dec 2010 14:00:44 GMT
Server: gws

So it seems that LWP::UserAgent works similarly with mechanize and WWW::Mechanize similarly to urllib. Nice.

Octavian

----- Original Message -----
From: "Karra" <>
Newsgroups: comp.lang.python
To: <python->
Sent: Wednesday, December 29, 2010 2:35 PM
Subject: Re: Making urllib2's POST 302 handle same as Perl LWP's behaviour


On Dec 29, 3:57 pm, Karra <ska...@gmail.com> wrote:

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


Out of frustration, I decided to give 'mechanize' a try. It came as an
awesome surprise that mechanize implements the exact api of urllib2 -
meaning all I had to do was a query-replace of urllib2 to mechanize,
and lo-and-behold I got what I wanted!

Which is to say, the behaviour of mechanize matches Perl's
LWP::UserAgent as afar as handling of POST redirects go.
--
http://mail.python.org/mailman/listinfo/python-list
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mvc same name same post rodchar ASP .Net 0 10-08-2009 08:43 PM
session variables - how to handle parallel access by the same user tothe same page Stephan Steiner ASP .Net 1 09-24-2009 03:10 AM
New User - Using tutorial and Docs - POST returns 302 Found JohnV Python 7 03-06-2009 06:53 PM
urllib2: handle an error (302) Antoni Villalonga Python 2 09-11-2007 12:01 PM
Re: httpconnection class handle 302 redirect? Laszlo Zsolt Nagy Python 0 12-07-2004 05:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57