View Single Post
Old 03-02-2011, 07:01 AM   #37
ldolse
Wizard
ldolse is an accomplished Snipe hunter.ldolse is an accomplished Snipe hunter.ldolse is an accomplished Snipe hunter.ldolse is an accomplished Snipe hunter.ldolse is an accomplished Snipe hunter.ldolse is an accomplished Snipe hunter.ldolse is an accomplished Snipe hunter.ldolse is an accomplished Snipe hunter.ldolse is an accomplished Snipe hunter.ldolse is an accomplished Snipe hunter.ldolse is an accomplished Snipe hunter.
 
Posts: 1,337
Karma: 123457
Join Date: Apr 2009
Location: Malaysia
Device: PRS-650, iPhone
I found a solution, not sure if it's the best one, but it's working. Figured out how to initialize a new cookiejar, copied the good cookie into that. Opened the bad page (corrupting the cookiejar) and replaced the corrupted cookiejar with the clean one.

Code:
    import copy

    goodcookies = br._ua_handlers['_cookies'].cookiejar
    clean_cj = mechanize.CookieJar()
    cookies_to_copy = []
    for cookie in goodcookies:
        copied_cookie = copy.deepcopy(cookie)
        cookies_to_copy.append(copied_cookie)
    for copied_cookie in cookies_to_copy:
        clean_cj.set_cookie(copied_cookie)
    
    # request that corrupts the cookiejar
    br.open(q_init_search)
    
    br.set_cookiejar(clean_cj)

Last edited by ldolse; 03-03-2011 at 01:33 AM. Reason: latest fix
ldolse is offline   Reply With Quote