import mechanize
import urllib2
import time
import cookielib
import requests
username = 'user@gmail.com' # your username/email
password = 'pwd' # your password
br = mechanize.Browser()
# browser settings (used to emulate a 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 the form
br['email'] = username
br['password'] = password
br.submit() # submit the login data
# set cookies
cookies = cookielib.LWPCookieJar()
br.set_cookiejar(cookies)
print cookies
temp_jar=br.set_cookiejar(cookies)
print(br.response().read()) # print the 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
基本上我有 2 个 URL,我必须先登录(“ https://sso.openx.com/login/login ”),只有在使用正确的凭据登录后,我才能访问另一个 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 ") 我必须获取返回的 json通过网址
所以基本上我想要一个持久会话,我可以登录然后访问我能够成功登录但无法使用相同会话或cookie或两个调用之间的任何其他持久性方法调用另一个url