urllib2 and Set-Cookie with "302 Moved temporarily"

  • Thread starter =?ISO-8859-1?Q?Eino_M=E4kitalo?=
  • Start date
?

=?ISO-8859-1?Q?Eino_M=E4kitalo?=

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
 
F

Fredrik Lundh

Eino Mäkitalo said:
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top