urllib2 and HTTP 302

John J. Lee jjlee at reportlab.com
Mon Jun 5 21:14:59 CEST 2006


jjl at pobox.com (John J. Lee) writes:

> Laszlo Nagy <gandalf at designaproduct.biz> writes:
> [...]
> > how can I return the redirection URL?
> > I tried to get this information from the exception but I could not. Is 
> > it possible to read it from the openerdirector?
> > Any suggestions?
> > 
> > 
> >             try:
> >                self.post_multipart(
> >                     url,
> >                     [('uploadType','Inventory')],
> >                     [('uploadFileName','inv.txt',fdata)]
> >                 )
> >             except urllib2.HTTPError, e:
> >                 if e.code == 302:
> >                     return "I would like to get the URL to be redirected 
> > to...."
> >                 else:
> >                     raise                        
> 
> redirected_url = e.geturl()

Sorry, didn't read that properly.

If your OpenerDirector instance has an HTTPRedirectHandler, you should
be able to get the final redirected URL the way I said (using
.geturl()).  If it doesn't (which I assume is true in your case -- why
else would you be checking for a 302 status), well, you just have to
do exactly the same thing that HTTPRedirectHandler does :-) Use the
source luke.


John



More information about the Python-list mailing list