Python httplib2.ServerNotFoundError() Examples

The following are 21 code examples of httplib2.ServerNotFoundError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module httplib2 , or try the search function .
Example #1
Source File: app.py    From moa with MIT License 7 votes vote down vote up
def instagram_activate():
    client_id = app.config['INSTAGRAM_CLIENT_ID']
    client_secret = app.config['INSTAGRAM_SECRET']
    redirect_uri = url_for('instagram_oauthorized', _external=True)
    # app.logger.info(redirect_uri)

    scope = ["basic"]
    api = InstagramAPI(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)

    try:
        redirect_uri = api.get_authorize_login_url(scope=scope)
    except ServerNotFoundError as e:
        flash(f"There was a problem connecting to Instagram. Please try again")
        return redirect(url_for('index'))
    else:
        return redirect(redirect_uri) 
Example #2
Source File: plugin.py    From GloboNetworkAPI with Apache License 2.0 6 votes vote down vote up
def _send_request(self, data, uri):
        """Send requests for the equipment"""
        try:
            count = 0
            while count < self.MAX_RETRIES:
                resp, content = self.HTTP.request(uri,
                                                  method="POST",
                                                  headers=self.HEADERS,
                                                  body=dumps(data))
                if resp.status == status.HTTP_200_OK:
                    return content
                count += 1
                if count >= self.MAX_RETRIES:
                    raise MaxRetryAchieved(self.equipment.nome)
        except MaxRetryAchieved as error:
            log.error(error)
            raise error
        except socket.error as error:
            log.error('Error in socket connection: %s' % error)
            raise error
        except httplib2.ServerNotFoundError as error:
            log.error(
                'Error: %s. Check if the restserver is enabled in %s' %
                (error, self.equipment.nome))
            raise error
        except Exception as error:
            log.error('Error: %s' % error)
            raise error 
Example #3
Source File: gmail_refresh.py    From alfred-gmail with MIT License 6 votes vote down vote up
def refresh_cache(labels=None):
    labels = labels if labels is not None else config.SYSTEM_LABELS.keys()
    flow = flow_from_clientsecrets(
        config.CLIENT_SECRET_FILE, scope=config.OAUTH_SCOPE)
    http = httplib2.Http()

    try:
        credentials = OAuth2Credentials.from_json(
            WF.get_password('gmail_credentials'))
        if credentials is None or credentials.invalid:
            credentials = run_flow(flow, PseudoStorage(), http=http)
            WF.save_password('gmail_credentials', credentials.to_json())
            WF.logger.debug('Credentials securely updated')

        http = credentials.authorize(http)
        gmail_service = build('gmail', 'v1', http=http)

        for label in labels:
            WF.cache_data('gmail_%s' %
                          label.lower(), get_list(http, gmail_service, label))
            sleep(2)
        if not WF.cached_data_fresh('gmail_labels', max_age=300):
            WF.cache_data('gmail_labels', get_labels(gmail_service))

    except PasswordNotFound:
        WF.logger.debug('Credentials not found')
        credentials = run_flow(flow, PseudoStorage(), http=http)
        WF.save_password('gmail_credentials', credentials.to_json())
        WF.logger.debug('New Credentials securely saved')

    except httplib2.ServerNotFoundError:
        WF.logger.debug('ServerNotFoundError') 
Example #4
Source File: google.py    From pghoard with Apache License 2.0 6 votes vote down vote up
def _init_google_client(self):
        start_time = time.monotonic()
        delay = 2
        while True:
            try:
                # sometimes fails: httplib2.ServerNotFoundError: Unable to find the server at www.googleapis.com
                return build("storage", "v1", credentials=self.google_creds)
            except (httplib2.ServerNotFoundError, socket.timeout):
                if time.monotonic() - start_time > 600:
                    raise

            # retry on DNS issues
            time.sleep(delay)
            delay = delay * 2 
Example #5
Source File: gdriveutils.py    From calibre-web with GNU General Public License v3.0 6 votes vote down vote up
def listRootFolders():
    try:
        drive = getDrive(Gdrive.Instance().drive)
        folder = "'root' in parents and mimeType = 'application/vnd.google-apps.folder' and trashed = false"
        fileList = drive.ListFile({'q': folder}).GetList()
    except ServerNotFoundError as e:
        log.info("GDrive Error %s" % e)
        fileList = []
    return fileList 
Example #6
Source File: http_wrapper_test.py    From apitools with Apache License 2.0 6 votes vote down vote up
def testDefaultExceptionHandler(self):
        """Ensures exception handles swallows (retries)"""
        mock_http_content = 'content'.encode('utf8')
        for exception_arg in (
                http_client.BadStatusLine('line'),
                http_client.IncompleteRead('partial'),
                http_client.ResponseNotReady(),
                socket.error(),
                socket.gaierror(),
                httplib2.ServerNotFoundError(),
                ValueError(),
                exceptions.RequestError(),
                exceptions.BadStatusCodeError(
                    {'status': 503}, mock_http_content, 'url'),
                exceptions.RetryAfterError(
                    {'status': 429}, mock_http_content, 'url', 0)):

            retry_args = http_wrapper.ExceptionRetryArgs(
                http={'connections': {}}, http_request=_MockHttpRequest(),
                exc=exception_arg, num_retries=0, max_retry_wait=0,
                total_wait_sec=0)

            # Disable time.sleep for this handler as it is called with
            # a minimum value of 1 second.
            with patch('time.sleep', return_value=None):
                http_wrapper.HandleExceptionsAndRebuildHttpConnections(
                    retry_args) 
Example #7
Source File: httplib2_test.py    From httplib2shim with MIT License 5 votes vote down vote up
def testGetViaHttpsKeyCert(self):
        #  At this point I can only test
        #  that the key and cert files are passed in
        #  correctly to httplib. It would be nice to have
        #  a real https endpoint to test against.
        http = httplib2.Http(timeout=2)

        http.add_certificate("akeyfile", "acertfile", "bitworking.org")
        try:
            (response, content) = http.request("https://bitworking.org", "GET")
        except AttributeError:
            self.assertEqual(
                http.connections["https:bitworking.org"].key_file, "akeyfile")
            self.assertEqual(
                http.connections[
                    "https:bitworking.org"].cert_file, "acertfile")
        except IOError:
            # Skip on 3.2
            pass

        try:
            (response, content) = http.request(
                "https://notthere.bitworking.org", "GET")
        except httplib2.ServerNotFoundError:
            self.assertEqual(
                http.connections["https:notthere.bitworking.org"].key_file,
                None)
            self.assertEqual(
                http.connections["https:notthere.bitworking.org"].cert_file,
                None)
        except IOError:
            # Skip on 3.2
            pass 
Example #8
Source File: httplib2_test.py    From httplib2shim with MIT License 5 votes vote down vote up
def testGetUnknownServer(self):
        self.http.force_exception_to_status_code = False
        try:
            self.http.request("http://fred.bitworking.org/")
            self.fail(
                'An httplib2.ServerNotFoundError Exception must be thrown on '
                'an unresolvable server.')
        except httplib2.ServerNotFoundError:
            pass

        # Now test with exceptions turned off
        self.http.force_exception_to_status_code = True

        (response, content) = self.http.request("http://fred.bitworking.org/")
        self.assertEqual(response['content-type'], 'text/plain')
        self.assertTrue(content.startswith(b"Unable to find"))
        self.assertEqual(response.status, 400) 
Example #9
Source File: stack.py    From spotty with MIT License 5 votes vote down vote up
def wait_stack_deleted(self, delay=15):
        stack = True
        while stack:
            try:
                stack = self.get_by_name(self._dm, self.name)
            except (ConnectionResetError, ServerNotFoundError):
                logging.warning('Connection problem')
                continue

            sleep(delay) 
Example #10
Source File: oauth2_client.py    From gcs-oauth2-boto-plugin with Apache License 2.0 5 votes vote down vote up
def _IsGCE():
  """Returns True if running on a GCE instance, otherwise False."""
  try:
    http = httplib2.Http()
    response, _ = http.request(METADATA_SERVER)
    return response.status == 200

  except (httplib2.ServerNotFoundError, socket.error):
    # We might see something like "No route to host" propagated as a socket
    # error. We might also catch transient socket errors, but at that point
    # we're going to fail anyway, just with a different error message. With
    # this approach, we'll avoid having to enumerate all possible non-transient
    # socket errors.
    return False
  except Exception as e:  # pylint: disable=broad-except
    LOG.warning("Failed to determine whether we're running on GCE, so we'll"
                "assume that we aren't: %s", e)
    return False

  return False 
Example #11
Source File: cleaner.py    From google-drive-trash-cleaner with GNU General Public License v3.0 4 votes vote down vote up
def main():
    flags = parse_cmdline()
    logger = configure_logs(flags.logfile)
    pageTokenFile = PageTokenFile(flags.ptokenfile)
    for i in range(RETRY_NUM):
        try:
            service = build_service(flags)
            pageToken = pageTokenFile.get()
            deletionList, pageTokenBefore, pageTokenAfter = \
                get_deletion_list(service, pageToken, flags)
            pageTokenFile.save(pageTokenBefore)
            listEmpty = delete_old_files(service, deletionList, flags)
        except client.HttpAccessTokenRefreshError:
            print('Authentication error')
        except httplib2.ServerNotFoundError as e:
            print('Error:', e)
        except TimeoutError:
            print('Timeout: Google backend error.')
            print('Retries unsuccessful. Abort action.')
            return
        else:
            break
        time.sleep(RETRY_INTERVAL)
    else:
        print("Retries unsuccessful. Abort action.")
        return
    
    if listEmpty:
        pageTokenFile.save(pageTokenAfter) 
Example #12
Source File: __init__.py    From httplib2shim with MIT License 4 votes vote down vote up
def _map_exception(e):
    """Maps an exception from urlib3 to httplib2."""
    if isinstance(e, urllib3.exceptions.MaxRetryError):
        if not e.reason:
            return e
        e = e.reason
    message = e.args[0] if e.args else ''
    if isinstance(e, urllib3.exceptions.ResponseError):
        if 'too many redirects' in message:
            return httplib2.RedirectLimit(message)
    if isinstance(e, urllib3.exceptions.NewConnectionError):
        if ('Name or service not known' in message or
                'nodename nor servname provided, or not known' in message):
            return httplib2.ServerNotFoundError(
                'Unable to find hostname.')
        if 'Connection refused' in message:
            return socket.error((errno.ECONNREFUSED, 'Connection refused'))
    if isinstance(e, urllib3.exceptions.DecodeError):
        return httplib2.FailedToDecompressContent(
            'Content purported as compressed but not uncompressable.',
            httplib2.Response({'status': 500}), '')
    if isinstance(e, urllib3.exceptions.TimeoutError):
        return socket.timeout('timed out')
    if isinstance(e, urllib3.exceptions.SSLError):
        return ssl.SSLError(*e.args)

    return e 
Example #13
Source File: test_oauth2_client.py    From gcs-oauth2-boto-plugin with Apache License 2.0 4 votes vote down vote up
def testIsGCEServerNotFound(self):
    self.mock_http.request.side_effect = httplib2.ServerNotFoundError()

    self.assertFalse(oauth2_client._IsGCE())
    self.mock_http.request.assert_called_once_with(
        oauth2_client.METADATA_SERVER) 
Example #14
Source File: http_wrapper.py    From apitools with Apache License 2.0 4 votes vote down vote up
def HandleExceptionsAndRebuildHttpConnections(retry_args):
    """Exception handler for http failures.

    This catches known failures and rebuilds the underlying HTTP connections.

    Args:
      retry_args: An ExceptionRetryArgs tuple.
    """
    # If the server indicates how long to wait, use that value.  Otherwise,
    # calculate the wait time on our own.
    retry_after = None

    # Transport failures
    if isinstance(retry_args.exc, (http_client.BadStatusLine,
                                   http_client.IncompleteRead,
                                   http_client.ResponseNotReady)):
        logging.debug('Caught HTTP error %s, retrying: %s',
                      type(retry_args.exc).__name__, retry_args.exc)
    elif isinstance(retry_args.exc, socket.error):
        logging.debug('Caught socket error, retrying: %s', retry_args.exc)
    elif isinstance(retry_args.exc, socket.gaierror):
        logging.debug(
            'Caught socket address error, retrying: %s', retry_args.exc)
    elif isinstance(retry_args.exc, socket.timeout):
        logging.debug(
            'Caught socket timeout error, retrying: %s', retry_args.exc)
    elif isinstance(retry_args.exc, httplib2.ServerNotFoundError):
        logging.debug(
            'Caught server not found error, retrying: %s', retry_args.exc)
    elif isinstance(retry_args.exc, ValueError):
        # oauth2client tries to JSON-decode the response, which can result
        # in a ValueError if the response was invalid. Until that is fixed in
        # oauth2client, need to handle it here.
        logging.debug('Response content was invalid (%s), retrying',
                      retry_args.exc)
    elif (isinstance(retry_args.exc, TokenRefreshError) and
          hasattr(retry_args.exc, 'status') and
          (retry_args.exc.status == TOO_MANY_REQUESTS or
           retry_args.exc.status >= 500)):
        logging.debug(
            'Caught transient credential refresh error (%s), retrying',
            retry_args.exc)
    elif isinstance(retry_args.exc, exceptions.RequestError):
        logging.debug('Request returned no response, retrying')
    # API-level failures
    elif isinstance(retry_args.exc, exceptions.BadStatusCodeError):
        logging.debug('Response returned status %s, retrying',
                      retry_args.exc.status_code)
    elif isinstance(retry_args.exc, exceptions.RetryAfterError):
        logging.debug('Response returned a retry-after header, retrying')
        retry_after = retry_args.exc.retry_after
    else:
        raise retry_args.exc
    RebuildHttpConnections(retry_args.http)
    logging.debug('Retrying request to url %s after exception %s',
                  retry_args.http_request.url, retry_args.exc)
    time.sleep(
        retry_after or util.CalculateWaitForRetry(
            retry_args.num_retries, max_wait=retry_args.max_retry_wait)) 
Example #15
Source File: app.py    From moa with MIT License 4 votes vote down vote up
def instagram_oauthorized():
    code = request.args.get('code', None)

    if code:

        client_id = app.config['INSTAGRAM_CLIENT_ID']
        client_secret = app.config['INSTAGRAM_SECRET']
        redirect_uri = url_for('instagram_oauthorized', _external=True)
        api = InstagramAPI(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)

        try:
            access_token = api.exchange_code_for_access_token(code)
        except OAuth2AuthExchangeError as e:
            flash("Instagram authorization failed")
            return redirect(url_for('index'))
        except ServerNotFoundError as e:
            flash("Instagram authorization failed")
            return redirect(url_for('index'))

        if 'bridge_id' in session:
            bridge = get_or_create_bridge(bridge_id=session['bridge_id'])

            if not bridge:
                pass  # this should be an error
        else:
            bridge = get_or_create_bridge()

        bridge.instagram_access_code = access_token[0]

        data = access_token[1]
        bridge.instagram_account_id = data['id']
        bridge.instagram_handle = data['username']

        user_api = InstagramAPI(access_token=bridge.instagram_access_code, client_secret=client_secret)

        try:
            latest_media, _ = user_api.user_recent_media(user_id=bridge.instagram_account_id, count=1)
        except Exception:
            latest_media = []

        if len(latest_media) > 0:
            bridge.instagram_last_id = datetime_to_timestamp(latest_media[0].created_time)
        else:
            bridge.instagram_last_id = 0

        db.session.commit()

    else:
        flash("Instagram authorization failed")

    return redirect(url_for('index')) 
Example #16
Source File: test_exceptions.py    From firebase-admin-python with Apache License 2.0 4 votes vote down vote up
def test_googleapiclient_connection_error(self):
        error = httplib2.ServerNotFoundError('Test error')
        firebase_error = _utils.handle_googleapiclient_error(error)
        assert isinstance(firebase_error, exceptions.UnavailableError)
        assert str(firebase_error) == 'Failed to establish a connection: Test error'
        assert firebase_error.cause is error
        assert firebase_error.http_response is None 
Example #17
Source File: _utils.py    From firebase-admin-python with Apache License 2.0 4 votes vote down vote up
def handle_googleapiclient_error(error, message=None, code=None, http_response=None):
    """Constructs a ``FirebaseError`` from the given googleapiclient error.

    This method is agnostic of the remote service that produced the error, whether it is a GCP
    service or otherwise. Therefore, this method does not attempt to parse the error response in
    any way.

    Args:
        error: An error raised by the googleapiclient module while making an HTTP call.
        message: A message to be included in the resulting ``FirebaseError`` (optional). If not
            specified the string representation of the ``error`` argument is used as the message.
        code: A GCP error code that will be used to determine the resulting error type (optional).
            If not specified the HTTP status code on the error response is used to determine a
            suitable error code.
        http_response: A requests HTTP response object to associate with the exception (optional).
            If not specified, one will be created from the ``error``.

    Returns:
        FirebaseError: A ``FirebaseError`` that can be raised to the user code.
    """
    if isinstance(error, socket.timeout) or (
            isinstance(error, socket.error) and 'timed out' in str(error)):
        return exceptions.DeadlineExceededError(
            message='Timed out while making an API call: {0}'.format(error),
            cause=error)
    if isinstance(error, httplib2.ServerNotFoundError):
        return exceptions.UnavailableError(
            message='Failed to establish a connection: {0}'.format(error),
            cause=error)
    if not isinstance(error, googleapiclient.errors.HttpError):
        return exceptions.UnknownError(
            message='Unknown error while making a remote service call: {0}'.format(error),
            cause=error)

    if not code:
        code = _http_status_to_error_code(error.resp.status)
    if not message:
        message = str(error)
    if not http_response:
        http_response = _http_response_from_googleapiclient_error(error)

    err_type = _error_code_to_exception_type(code)
    return err_type(message=message, cause=error, http_response=http_response) 
Example #18
Source File: http.py    From alfred-gmail with MIT License 4 votes vote down vote up
def _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args,
                   **kwargs):
  """Retries an HTTP request multiple times while handling errors.

  If after all retries the request still fails, last error is either returned as
  return value (for HTTP 5xx errors) or thrown (for ssl.SSLError).

  Args:
    http: Http object to be used to execute request.
    num_retries: Maximum number of retries.
    req_type: Type of the request (used for logging retries).
    sleep, rand: Functions to sleep for random time between retries.
    uri: URI to be requested.
    method: HTTP method to be used.
    args, kwargs: Additional arguments passed to http.request.

  Returns:
    resp, content - Response from the http request (may be HTTP 5xx).
  """
  resp = None
  content = None
  for retry_num in range(num_retries + 1):
    if retry_num > 0:
      # Sleep before retrying.
      sleep_time = rand() * 2 ** retry_num
      LOGGER.warning(
          'Sleeping %.2f seconds before retry %d of %d for %s: %s %s, after %s',
          sleep_time, retry_num, num_retries, req_type, method, uri,
          resp.status if resp else exception)
      sleep(sleep_time)

    try:
      exception = None
      resp, content = http.request(uri, method, *args, **kwargs)
    # Retry on SSL errors and socket timeout errors.
    except _ssl_SSLError as ssl_error:
      exception = ssl_error
    except socket.timeout as socket_timeout:
      # It's important that this be before socket.error as it's a subclass
      # socket.timeout has no errorcode
      exception = socket_timeout
    except socket.error as socket_error:
      # errno's contents differ by platform, so we have to match by name.
      if socket.errno.errorcode.get(socket_error.errno) not in {
        'WSAETIMEDOUT', 'ETIMEDOUT', 'EPIPE', 'ECONNABORTED'}:
        raise
      exception = socket_error
    except httplib2.ServerNotFoundError as server_not_found_error:
      exception = server_not_found_error

    if exception:
      if retry_num == num_retries:
        raise exception
      else:
        continue

    if not _should_retry_response(resp.status, content):
      break

  return resp, content 
Example #19
Source File: stack.py    From spotty with MIT License 4 votes vote down vote up
def wait_stack_done(self, delay=5):
        is_done = False
        while not is_done:
            try:
                stack = self.get_by_name(self._dm, self.name)
                is_done = stack.is_done
            except (ConnectionResetError, ServerNotFoundError):
                logging.warning('Connection problem')
                continue

            sleep(delay) 
Example #20
Source File: deployment.py    From spotty with MIT License 4 votes vote down vote up
def wait_resources(dm: DMClient, ce: CEClient, deployment_name: str, resource_messages: OrderedDict,
                   instance_resource_name: str, machine_name: str, output: AbstractOutputWriter, delay: int = 5):
    # make sure that the instance resource is in the messages list
    assert any(resource_name == instance_resource_name for resource_name, _ in resource_messages.items())

    created_resources = set()
    for resource_name, message in resource_messages.items():
        output.write('- %s...' % message)

        is_created = False
        while not is_created:
            sleep(delay)

            # get the resource info
            try:
                # check that the deployment is not failed
                stack = Stack.get_by_name(dm, deployment_name)
                if stack.error:
                    raise ValueError('Deployment "%s" failed.\n'
                                     'Error: %s' % (deployment_name, stack.error['message']))

                # check if the instance was preempted, terminated or deleted right after creation
                if instance_resource_name in created_resources:
                    instance = Instance.get_by_name(ce, machine_name)
                    if not instance or instance.is_terminated:
                        raise ValueError('Error: the instance was unexpectedly terminated. Please, check out the '
                                         'instance logs to find out the reason.\n')

                # get resource
                resource = DMResource.get_by_name(dm, deployment_name, resource_name)
            except (ConnectionResetError, ServerNotFoundError):
                logging.warning('Connection problem')
                continue

            # resource doesn't exist yet
            if not resource:
                continue

            # resource failed
            if resource.is_failed:
                error_msg = ('Error: ' + resource.error_message) if resource.error_message \
                    else 'Please, see Deployment Manager logs for the details.' % deployment_name

                raise ValueError('Deployment "%s" failed.\n%s' % (deployment_name, error_msg))

            # resource was successfully created
            is_created = resource.is_created

        created_resources.add(resource_name) 
Example #21
Source File: google_base.py    From dsub with Apache License 2.0 4 votes vote down vote up
def retry_api_check(exception, verbose):
  """Return True if we should retry. False otherwise.

  Args:
    exception: An exception to test for transience.
    verbose: If true, output retry messages

  Returns:
    True if we should retry. False otherwise.
  """
  if isinstance(exception, googleapiclient.errors.HttpError):
    if exception.resp.status in TRANSIENT_HTTP_ERROR_CODES:
      _print_retry_error(exception, verbose)
      return True

  if isinstance(exception, socket.error):
    if exception.errno in TRANSIENT_SOCKET_ERROR_CODES:
      _print_retry_error(exception, verbose)
      return True

  if isinstance(exception, google.auth.exceptions.RefreshError):
    _print_retry_error(exception, verbose)
    return True

  # For a given installation, this could be a permanent error, but has only
  # been observed as transient.
  if isinstance(exception, ssl.SSLError):
    _print_retry_error(exception, verbose)
    return True

  # This has been observed as a transient error:
  #   ServerNotFoundError: Unable to find the server at genomics.googleapis.com
  if isinstance(exception, ServerNotFoundError):
    _print_retry_error(exception, verbose)
    return True

  # Observed to be thrown transiently from auth libraries which use httplib2
  # Use the one from six because httlib no longer exists in Python3
  # https://docs.python.org/2/library/httplib.html
  if isinstance(exception, six.moves.http_client.ResponseNotReady):
    _print_retry_error(exception, verbose)
    return True

  return False