10
# url = 'https://graphite.ubunet.canonical.com/render/?target=statsd.production.*.resources.sys.cpu.*.%7Buser,system,iowait,idle%7D.value&format=raw&from=-1days'
11
# cookie_file = "cookies.dat"
14
self.browser = mechanize.Browser()
16
self.browser.set_handle_equiv(True)
17
self.browser.set_handle_gzip(True)
18
self.browser.set_handle_redirect(True)
19
self.browser.set_handle_referer(True)
20
self.browser.set_handle_robots(False)
21
self.browser.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(),
24
def fetch(self, url, cookie_file):
25
cookie_jar = cookielib.LWPCookieJar(cookie_file)
26
if os.path.exists(cookie_file):
27
cookie_jar.load(ignore_discard=True)
28
self.browser.set_cookiejar(cookie_jar)
30
self.browser.open(url)
31
if self.browser.geturl() == \
32
"https://graphite.ubunet.canonical.com/openid/+login":
33
# if self.browser.response().info()['Content-Type'] != 'text/plain':
35
cookie_jar.save(ignore_discard=True)
36
os.chmod(cookie_file, stat.S_IRUSR | stat.S_IWUSR)
38
return self.browser.response()
43
assert url == 'https://graphite.ubunet.canonical.com/openid/+login', url
44
assert br.title() == 'OpenID Authentication Required', br.title()
49
assert url == 'https://graphite.ubunet.canonical.com/openid/+login', url
50
assert br.title() == 'OpenID Authentication Required', br.title()
54
if not br.title().startswith("Authenticate to "):
56
assert url.startswith('https://login.ubuntu.com/'), url
57
assert url.endswith('/+decide'), url
58
assert br.title() == 'Log in', br.title()
61
br.form['email'] = raw_input ("Email address: ")
62
br.form['password'] = getpass.getpass(" Password: ")
64
if not br.title().startswith("Authenticate to "):
65
response = br.response().read()
66
assert 'To continue use 2 factor authentication' in response
67
twofer = raw_input("2-factor auth: ")
69
br.form['oath_token'] = twofer
72
assert url.startswith('https://login.ubuntu.com/'), url
73
assert url.endswith('/+decide'), url
74
assert br.title().startswith("Authenticate to "), br.title()
79
# assert br.geturl() == url, br.geturl()
80
# content_type = br.response().info()['Content-Type']
81
# assert content_type == 'text/plain', content_type
83
fetch = Fetcher().fetch
85
if __name__ == '__main__':
86
if len(sys.argv) != 3:
87
print "Usage: %s url cookie_file" % sys.argv[0]
89
print fetch(sys.argv[1], sys.argv[2]).read()