Need to create a persistence session in python using mechanize browser instance -


import mechanize import urllib2 import time import cookielib import requests  username = 'user@gmail.com'   # username/email password = 'pwd'   # password  br = mechanize.browser()  # browser settings (used emulate browser) br.set_handle_equiv(true) br.set_handle_redirect(true) br.set_handle_referer(true) br.set_handle_robots(false) br.set_debug_http(false) br.set_debug_responses(false) br.set_debug_redirects(false) br.set_handle_refresh(mechanize._http.httprefreshprocessor(), max_time = 1) br.addheaders = [('user-agent', 'mozilla/5.0 (x11; u; linux i686; en-us; rv:1.9.0.1) gecko/2008071615 fedora/3.0.1-1.fc9 firefox/3.0.1')]  br.open('https://sso.openx.com/login/login') # open twitter  br.select_form(nr=0) # select form  br['email'] = username br['password'] = password br.submit() # submit login data     # set cookies cookies = cookielib.lwpcookiejar() br.set_cookiejar(cookies) print cookies temp_jar=br.set_cookiejar(cookies)  print(br.response().read()) # print response  br1=mechanize.browser() br1.set_cookiejar(cookies) br1.set_handle_robots(false) r=br1.open('http://ox-ui.admin.one97adworks.com/ox/3.0/a/report/run?&report_format=json&end_date=2013-06-01&report=order_perf&do_break=&rollup=hourly&start_date=2013-06-01') print r.text 

basically have 2 url's have login in first("https://sso.openx.com/login/login") after logging in correct credentials have access other url ("http://ox-ui.admin.one97adworks.com/ox/3.0/a/report/run?&report_format=json&end_date=2013-06-01&report=order_perf&do_break=&rollup=hourly&start_date=2013-06-01") , have fetch json returned url

so want persistent session can login , access other url able login haven't been able call other url using same session or cookies or other persistence method between 2 calls

try define cookiejar before open web page. , use 1 instance of mechanize


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -