Use the output of logs in the execution of a program
- by myle
When I try to create a specific object, the program crashes. However, I use a module (mechanize) which logs useful information just before the crash. If I had somehow this information available I could avoid it.
Is there any way to use the information which is logged (when I use the function set_debug_redirects) during the normal execution of the program?
Just to be a bit more specific, I try to emulate the login behavior in a webpage. The program crashes because it can't handle a specific Following HTTP-EQUIV=REFRESH to <omitted_url>. Given this url, which is available in the logs but not as part of the exception which is thrown, I could visit this page and complete successfully the login process.
Any other suggestions that may solve the problem are welcomed.
It follows the code so far.
SERVICE_LOGIN_BOX_URL = "https://www.google.com/accounts/ServiceLoginBox?service=adsense<mpl=login&ifr=true&rm=hide&fpui=3&nui=15&alwf=true&passive=true&continue=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&followup=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&hl=en_US"
def init_browser():
# Browser
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(False)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(True)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=30.0, honor_time=False)
# Want debugging messages?
#br.set_debug_http(True)
br.set_debug_redirects(True)
#br.set_debug_responses(True)
return br
def adsense_login(login, password):
br = init_browser()
r = br.open(SERVICE_LOGIN_BOX_URL)
html = r.read()
# Select the first (index zero) form
br.select_form(nr=0)
br.form['Email'] = login
br.form['Passwd'] = password
br.submit()
req = br.click_link(text='click here to continue')
try:
# this is where it crashes
br.open(req)
except HTTPError, e:
sys.exit("post failed: %d: %s" % (e.code, e.msg))
return br