I am using liburl2 with CookieJar / HTTPCookieProcessor in an attempt to simulate a page login to automate loading.
I saw some questions and answers to this, but nothing solves my problem. I lose my cookie when I simulate a login that ends with a 302 redirect. Answer 302 is where the cookie is set by the server, but urllib2 HTTPCookieProcessor does not seem to save the cookie during the redirect. I tried to create an HTTPRedirectHandler class to ignore the redirect, but that didn't seem to help. I tried calling the CookieJar globally to process cookies from the HTTPRedirectHandler, but 1. This did not work (because I was processing the header from the redirector, and the CookieJar function that I used, extract_cookies, needed a full request) and 2. This is an ugly way Deal with it.
I will probably need some guidance on this, since I'm pretty green with Python. I think I basically bark on the right tree here, but maybe focus on the wrong branch.
cj = cookielib.CookieJar() cookieprocessor = urllib2.HTTPCookieProcessor(cj) class MyHTTPRedirectHandler(urllib2.HTTPRedirectHandler): def http_error_302(self, req, fp, code, msg, headers): global cj cookie = headers.get("set-cookie") if cookie:
Addition: I also tried to use a machine operator, but to no avail. This is probably a new question, but I will present it here, as it is the same final goal:
This simple code using mechanization when used with 302 emitting URLs (http://fxfeeds.mozilla.com/firefox/headlines.xml) - note that the same behavior occurs when set_handle_robots (False) is not used. I just wanted to make sure this is not the case:
import urllib2, mechanize browser = mechanize.Browser() browser.set_handle_robots(False) opener = mechanize.build_opener(*(browser.handlers)) r = opener.open("http://fxfeeds.mozilla.com/firefox/headlines.xml")
Output:
Traceback (most recent call last): File "redirecttester.py", line 6, in <module> r = opener.open("http://fxfeeds.mozilla.com/firefox/headlines.xml") File "build/bdist.macosx-10.6-universal/egg/mechanize/_opener.py", line 204, in open File "build/bdist.macosx-10.6-universal/egg/mechanize/_urllib2_fork.py", line 457, in http_response File "build/bdist.macosx-10.6-universal/egg/mechanize/_opener.py", line 221, in error File "build/bdist.macosx-10.6-universal/egg/mechanize/_urllib2_fork.py", line 332, in _call_chain File "build/bdist.macosx-10.6-universal/egg/mechanize/_urllib2_fork.py", line 571, in http_error_302 File "build/bdist.macosx-10.6-universal/egg/mechanize/_opener.py", line 188, in open File "build/bdist.macosx-10.6-universal/egg/mechanize/_mechanize.py", line 71, in http_request AttributeError: OpenerDirector instance has no attribute '_add_referer_header'
Any ideas?