def query(self, service, num=0, timeout=10): """Query Notaries and return NotaryResponses instance For any Notary not responding, a None will be in the array. num specifies the number of Notaries to query. If 0, all notaries are queried. timeout is the timeout in seconds""" if num == 0: to_query = self else: if num > len(self): raise ValueError("Too many notaries requested (%s > %s)" % (num, len(self))) to_query = random.sample(self, num) responses = NotaryResponses() dispatchers = [] # Use own map here for thread safety map = {} for notary in to_query: self.logger.debug("Querying %s about %s..." % (notary, service)) dispatchers.append((notary, HTTP_dispatcher(notary.get_url(service), map=map))) self.logger.debug("Calling asyncore.loop()") timed_asyncore.loop_with_timeout(timeout=timeout, map=map) self.logger.debug("asyncore.loop() done.") for notary, dispatcher in dispatchers: try: response = dispatcher.get_response() xml = response.read() self.logger.debug("Validating response from %s (%d bytes)" % (notary, len(xml))) response = NotaryResponse(xml) notary.verify_response(response, service) self.logger.debug("Response signature verified") responses.append(response) except EOFError as e: self.logger.error("Failed to get response from %s: %s" % (notary, str(e))) responses.append(None) except httplib.BadStatusLine as e: self.logger.error("Failed to parse response from %s, bad status: %s" % (notary, e)) responses.append(None) except NotaryException as e: self.logger.error("Error validating response from %s: %s" % (notary, e)) responses.append(None) except Exception as e: self.logger.error("Unknown error handling response from %s: %s" % (notary, e)) responses.append(None) return responses
def __init__(self, notary, service, map=None): self.protocol = notary.get_protocol(service) HTTP_dispatcher.__init__(self, self.protocol.get_url(), map=map)
def get_response(self): """Return NotaryResponse instance""" response_fd = HTTP_dispatcher.get_response(self) data = response_fd.read() response = self.protocol.parse_response(data) return response