Re: urllib2 and HTTP 302

Discussion in 'Python' started by John J. Lee, Jun 1, 2006.

  1. John J. Lee

    John J. Lee Guest

    Laszlo Nagy <> 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()


    John
     
    John J. Lee, Jun 1, 2006
    #1
    1. Advertisements

  2. John J. Lee

    John J. Lee Guest

    (John J. Lee) writes:

    > Laszlo Nagy <> 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
     
    John J. Lee, Jun 5, 2006
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hendy
    Replies:
    0
    Views:
    919
    hendy
    Dec 20, 2005
  2. =?ISO-8859-1?Q?Eino_M=E4kitalo?=

    urllib2 and Set-Cookie with "302 Moved temporarily"

    =?ISO-8859-1?Q?Eino_M=E4kitalo?=, Dec 13, 2004, in forum: Python
    Replies:
    2
    Views:
    920
    =?ISO-8859-1?Q?Eino_M=E4kitalo?=
    Dec 13, 2004
  3. Laszlo Nagy

    urllib2 and HTTP 302

    Laszlo Nagy, May 26, 2006, in forum: Python
    Replies:
    0
    Views:
    480
    Laszlo Nagy
    May 26, 2006
  4. Antoni Villalonga

    urllib2: handle an error (302)

    Antoni Villalonga, Sep 11, 2007, in forum: Python
    Replies:
    2
    Views:
    1,139
    Antoni Villalonga
    Sep 11, 2007
  5. Gilles Ganault

    [urllib2] 302 -> can't get cookie

    Gilles Ganault, Jan 7, 2009, in forum: Python
    Replies:
    1
    Views:
    770
    Gabriel Genellina
    Jan 13, 2009
  6. hendy
    Replies:
    0
    Views:
    413
    hendy
    Dec 20, 2005
  7. hendy
    Replies:
    1
    Views:
    427
    Bob Barrows [MVP]
    Dec 20, 2005
  8. David Morgan
    Replies:
    1
    Views:
    480
    raj chahal
    Feb 24, 2006
Loading...