urllib2 and Set-Cookie with "302 Moved temporarily"

Discussion in 'Python' started by =?ISO-8859-1?Q?Eino_M=E4kitalo?=, Dec 13, 2004.

  1. It seems that urrlib2 default redirection does not allow me to handle
    Cookies. Service I'm trying seems to use IP switcher and session id's
    with cookies. After successful login it changes session id
    (PD-H_SESSION-ID) in 302 Moved temporarily. Urllib2 is so clever that it
    handles redirection but with wrong cookies.


    Some hookings? Just own version from source.
    What is the most lazy way to handle this. Older urllib?

    Eino

    ---------------------------------
    HTTP/1.x 302 Moved Temporarily
    Set-Cookie: BIGipServerWWW511_HTTP_Pool=1829440010.20480.0000;
    expires=Mon, 13-Dec-2004 11:55:59 GMT; path=/
    Set-Cookie:
    PD-H-SESSION-ID=4_w5sBH4QGJ+UqZ0nfWTduFl4yYQj8WToCPG3PO-NPo9sAAslb; Path=/
    Set-Cookie:
    PD-H-SESSION-ID=4_w5sBH4QGJ+UqZ0nfWTduFl4yYQj8WToCPG3PO-NPo9sAAslb; Path=/
    Date: Mon, 13 Dec 2004 11:25:59 GMT
    Message-Id: c365e552-4cf9-11d9-ab36-0a0a0b61aa77
    Cache-Control: no-cache
    Pragma: no-cache
    Connection: close
    Location: https://xxx/yyy/xxx/RepresentationApp
    Content-Type: text/html
    =?ISO-8859-1?Q?Eino_M=E4kitalo?=, Dec 13, 2004
    #1
    1. Advertising

  2. "Eino Mäkitalo" <> wrote:

    > It seems that urrlib2 default redirection does not allow me to handle
    > Cookies. Service I'm trying seems to use IP switcher and session id's with cookies. After
    > successful login it changes session id (PD-H_SESSION-ID) in 302 Moved temporarily.


    and adds a new cookie.

    > Urllib2 is so clever that it handles redirection but with wrong cookies.


    with the old cookies, that is. that's stupid.

    here's an ugly hack for the old urllib that looks for set-cookie headers in
    redirects, and adds corresponding cookie headers to the new request:

    import urllib

    class my_url_opener(urllib.FancyURLopener):

    def http_error_302(self, *args):
    headers = args[4]
    # print headers # <-- uncomment to see the headers
    cookie = headers.get("set-cookie")
    if cookie:
    # this is ugly
    self.addheaders.append(("Cookie", cookie.split(";")[0]))
    return urllib.FancyURLopener.http_error_302(self, *args)

    myurlopen = my_url_opener().open

    myurlopen("http://www.google.com")

    </F>
    Fredrik Lundh, Dec 13, 2004
    #2
    1. Advertising

  3. Fredrik Lundh wrote:
    > "Eino Mäkitalo" <> wrote:
    > # this is ugly


    Yes, but short and easy. Thanks.
    =?ISO-8859-1?Q?Eino_M=E4kitalo?=, Dec 13, 2004
    #3
    1. Advertising

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. Dave I
    Replies:
    1
    Views:
    1,093
    Ryan Stewart
    Feb 16, 2004
  2. Josef Cihal
    Replies:
    0
    Views:
    717
    Josef Cihal
    Sep 5, 2005
  3. Replies:
    5
    Views:
    547
    Aaron Watters
    May 2, 2008
  4. Replies:
    2
    Views:
    458
  5. Phil Powell
    Replies:
    2
    Views:
    171
    Grant Wagner
    Sep 5, 2003
Loading...

Share This Page