By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,570 Members | 1,306 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,570 IT Pros & Developers. It's quick & easy.

urllib2 and Set-Cookie with "302 Moved temporarily"

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Eino Mäkitalo" <ei**@iki.fi> 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>

Jul 18 '05 #2

P: n/a
Fredrik Lundh wrote:
"Eino Mäkitalo" <ei**@iki.fi> wrote:
# this is ugly


Yes, but short and easy. Thanks.
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.