def __init__(self, server, username = None, password = None): self.server = server self.username = username self.password = password self.token = None self.cache = GetURL(debug=True) self._auth()
class AMEE: def __init__(self, server, username = None, password = None): self.server = server self.username = username self.password = password self.token = None self.cache = GetURL(debug=True) self._auth() def _auth(self, refresh = False): assert self.username!=None and self.password != None if refresh: max_age = 0 else: max_age = -1 data = self.cache.get("http://%s/auth"%self.server, data={"username":self.username,"password":self.password},headers={"Accept":"application/xml"}, max_age = max_age) self.token = data.headers.cookies()["authToken"] return True def _get_authed(self,uri): try: assert self.token != None data = self.cache.get("http://%s/%s"%(self.server,uri),headers={"Accept":"application/xml","Authtoken":self.token}, max_age = -1).read() except URLTimeoutError, e: if e.code == 401: # only do this once just in case the token was an old cached one self._auth(refresh=True) assert self.token != None data = self.cache.get("http://%s/%s"%(self.server,uri),headers={"Accept":"application/xml","Authtoken":self.token}, max_age = -1).read() else: raise try: xml = fromstring(data) except ExpatError: open("dump","w").write(data) raise return ElementTree(xml).getroot()