Beispiel #1
0
 def _send(self, call):
     call_json = json.dumps(call).encode('utf-8')
     url_file = None
     try:
         try:
             url_file = urlopen(self.url, call_json)
             response_json = url_file.read().decode('utf-8')
         finally:
             if url_file is not None:
                 url_file.close()
     except Exception as e: # TODO: Exceptions are different for Python 2/3
         raise ServerUnreachableError(self.url, e)
     return response_json
Beispiel #2
0
 def _send(self, call):
     call_json = json.dumps(call)
     # Py2 returns bytes, Py3 returns unicode str
     if isinstance(call_json, text_type):
         call_json = call_json.encode('utf-8')
     url_file = None
     try:
         try:
             url_file = urlopen(self.url, call_json)
             response_json = url_file.read().decode('utf-8')
         finally:
             if url_file is not None:
                 url_file.close()
     except Exception as e:
         raise ServerUnreachableError(self.url, e)
     return response_json
Beispiel #3
0
def _wait_on_url(url, closing_event, log):
    """Wait for a long running http request, and respond to a closing event"""

    def do_wait(wait_seconds):
        """Wait for n seconds, or until closing event is set"""
        for _ in xrange(wait_seconds):
            if closing_event.is_set():
                return True
            sleep(1)
        return False

    while not closing_event.is_set():
        url_file = None
        try:
            try:
                url_file = urlopen(url)
            except HTTPError as e:
                # Server probably down or some other connectivity issue
                log.warning("failed to connect to {} ({}), retry in {} seconds".format(url, e, CONNECT_WAIT))
                if do_wait(CONNECT_WAIT):
                    break
                continue
            except Exception:
                # Something else
                log.exception("failed to connect to {}, retry in {} seconds".format(url, CONNECT_WAIT))
                if do_wait(CONNECT_WAIT):
                    break
                continue
            try:
                # This blocks
                # Don't know of a simple way to make it non-blocking with https
                response = url_file.read()
            except:
                log.exception("unable to read response from {}".format(url))
                if do_wait(CONNECT_WAIT):
                    break
                continue

            return response
        finally:
            if url_file is not None:
                url_file.close()
    return None