Python six.moves.http_client.BadStatusLine() Examples

The following are 12 code examples of six.moves.http_client.BadStatusLine(). 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 six.moves.http_client , or try the search function .
Example #1
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 #2
Source File: http.py    From pytest-plugins with MIT License 6 votes vote down vote up
def get(self, path, as_json=False, attempts=25):
        """ Queries the server using requests.GET and returns the response object. 
        
        Parameters
        ----------
        path :  `str`
            Path to the resource, relative to 'http://hostname:port/'
        as_json :  `bool`
            Returns the json object if True. Defaults to False.
        attempts: `int`
            This function will retry up to `attempts` times on connection errors, to handle 
            the server still waking up. Defaults to 25.
        """
        e = None
        for i in range(attempts):
            try:
                with self.handle_proxy():
                    returned = requests.get('http://%s:%d/%s' % (self.hostname, self.port, path))
                return returned.json() if as_json else returned
            except (http_client.BadStatusLine, requests.ConnectionError) as e:
                time.sleep(int(i) / 10)
                pass
        raise e 
Example #3
Source File: http.py    From pytest-plugins with MIT License 6 votes vote down vote up
def post(self, path, data=None, attempts=25, as_json=False, headers=None):
        """ Posts data to the server using requests.POST and returns the response object. 
        
        Parameters
        ----------
        path :  `str`
            Path to the resource, relative to 'http://hostname:port/'
        as_json :  `bool`
            Returns the json response if True. Defaults to False.
        attempts: `int`
            This function will retry up to `attempts` times on connection errors, to handle 
            the server still waking up. Defaults to 25.
        headers: `dict`
            Optional HTTP headers.
        """
        e = None
        for i in range(attempts):
            try:
                with self.handle_proxy():
                    returned = requests.post('http://%s:%d/%s' % (self.hostname, self.port, path), data=data, headers=headers)
                return returned.json() if as_json else returned
            except (http_client.BadStatusLine, requests.ConnectionError) as e:
                time.sleep(int(i) / 10)
                pass
        raise e 
Example #4
Source File: package_index.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def open_url(self, url, warning=None):
        if url.startswith('file:'):
            return local_open(url)
        try:
            return open_with_auth(url, self.opener)
        except (ValueError, http_client.InvalidURL) as v:
            msg = ' '.join([str(arg) for arg in v.args])
            if warning:
                self.warn(warning, msg)
            else:
                raise DistutilsError('%s %s' % (url, msg))
        except urllib.error.HTTPError as v:
            return v
        except urllib.error.URLError as v:
            if warning:
                self.warn(warning, v.reason)
            else:
                raise DistutilsError("Download error for %s: %s"
                                     % (url, v.reason))
        except http_client.BadStatusLine as v:
            if warning:
                self.warn(warning, v.line)
            else:
                raise DistutilsError(
                    '%s returned a bad status line. The server might be '
                    'down, %s' %
                    (url, v.line)
                )
        except (http_client.HTTPException, socket.error) as v:
            if warning:
                self.warn(warning, v)
            else:
                raise DistutilsError("Download error for %s: %s"
                                     % (url, v)) 
Example #5
Source File: test_response.py    From wextracto with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_read_non_integer_code():
    with pytest.raises(BadStatusLine):
        build_response(content, code='X') 
Example #6
Source File: test_response.py    From wextracto with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_read_bad_protocol_version():
    with pytest.raises(BadStatusLine):
        build_response(content, protocol_version='HTTP/X') 
Example #7
Source File: subscriptionmanager.py    From virt-who with GNU General Public License v2.0 5 votes vote down vote up
def hypervisorHeartbeat(self, config, options=None):
        """
        Send heart beat to candlepin server
        :param config: reference on configuration
        :param options: other options
        """
        if options:
            named_options = NamedOptions()
            for key, value in options['global'].items():
                setattr(named_options, key, value)
        else:
            named_options = None

        try:
            connection = self._connect(config)
            result = connection.hypervisorHeartbeat(config['owner'], named_options)
        except BadStatusLine:
            raise ManagerError("Communication with subscription manager interrupted")
        except rhsm_connection.RateLimitExceededException as e:
            raise ManagerThrottleError(e.retry_after)
        except rhsm_connection.GoneException:
            raise ManagerError("Communication with subscription manager failed: consumer no longer exists")
        except rhsm_connection.ConnectionException as e:
            if hasattr(e, 'code'):
                raise ManagerError("Communication with subscription manager failed with code %d: %s" % (e.code, str(e)))
            raise ManagerError("Communication with subscription manager failed: %s" % str(e))

        return result 
Example #8
Source File: subscriptionmanager.py    From virt-who with GNU General Public License v2.0 5 votes vote down vote up
def check_report_state(self, report):
        # BZ 1554228
        job_id = str(report.job_id)
        self._connect(report.config)
        self.logger.debug('Checking status of job %s', job_id)
        try:
            result = self.connection.getJob(job_id)
        except BadStatusLine:
            raise ManagerError("Communication with subscription manager interrupted")
        except rhsm_connection.RateLimitExceededException as e:
            raise ManagerThrottleError(e.retry_after)
        except rhsm_connection.ConnectionException as e:
            if hasattr(e, 'code'):
                raise ManagerError("Communication with subscription manager failed with code %d: %s" % (e.code, str(e)))
            raise ManagerError("Communication with subscription manager failed: %s" % str(e))
        state = STATE_MAPPING.get(result['state'], AbstractVirtReport.STATE_FAILED)
        report.state = state
        if state not in (AbstractVirtReport.STATE_FINISHED,
                         AbstractVirtReport.STATE_CANCELED,
                         AbstractVirtReport.STATE_FAILED):
            self.logger.debug('Job %s not finished', job_id)
        else:
            # log completed job status
            result_data = result.get('resultData', {})
            if not result_data:
                self.logger.warning("Job status report without resultData: %s", result)
                return
            if isinstance(result_data, string_types):
                self.logger.warning("Job status report encountered the following error: %s", result_data)
                return
            for fail in result_data.get('failedUpdate', []):
                self.logger.error("Error during update list of guests: %s", str(fail))
            self.logger.debug("Number of mappings unchanged: %d", len(result_data.get('unchanged', [])))
            self.logger.info("Mapping for config \"%s\" updated", report.config.name) 
Example #9
Source File: package_index.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def open_url(self, url, warning=None):
        if url.startswith('file:'):
            return local_open(url)
        try:
            return open_with_auth(url, self.opener)
        except (ValueError, http_client.InvalidURL) as v:
            msg = ' '.join([str(arg) for arg in v.args])
            if warning:
                self.warn(warning, msg)
            else:
                raise DistutilsError('%s %s' % (url, msg))
        except urllib.error.HTTPError as v:
            return v
        except urllib.error.URLError as v:
            if warning:
                self.warn(warning, v.reason)
            else:
                raise DistutilsError("Download error for %s: %s"
                                     % (url, v.reason))
        except http_client.BadStatusLine as v:
            if warning:
                self.warn(warning, v.line)
            else:
                raise DistutilsError(
                    '%s returned a bad status line. The server might be '
                    'down, %s' %
                    (url, v.line)
                )
        except (http_client.HTTPException, socket.error) as v:
            if warning:
                self.warn(warning, v)
            else:
                raise DistutilsError("Download error for %s: %s"
                                     % (url, v)) 
Example #10
Source File: subscriptionmanager.py    From virt-who with GNU General Public License v2.0 4 votes vote down vote up
def hypervisorCheckIn(self, report, options=None):
        """ Send hosts to guests mapping to subscription manager. """
        connection = self._connect(report.config)

        is_async = self._is_rhsm_server_async(report, connection)
        serialized_mapping = self._hypervisor_mapping(report, is_async, connection)
        self.logger.debug("Host-to-guest mapping being sent to '{owner}': {mapping}".format(
                          owner=report.config['owner'],
                          mapping=json.dumps(serialized_mapping, indent=4)))

        # All subclasses of ConfigSection use dictionary like notation,
        # but RHSM uses attribute like notation
        if options:
            named_options = NamedOptions()
            for key, value in options['global'].items():
                setattr(named_options, key, value)
        else:
            named_options = None

        try:
            try:
                result = self.connection.hypervisorCheckIn(
                    report.config['owner'],
                    '',
                    serialized_mapping,
                    options=named_options)  # pylint:disable=unexpected-keyword-arg
            except TypeError:
                # This is temporary workaround until the options parameter gets implemented
                # in python-rhsm
                self.logger.debug(
                    "hypervisorCheckIn method in python-rhsm doesn't understand options parameter, ignoring"
                )
                result = self.connection.hypervisorCheckIn(report.config['owner'], '', serialized_mapping)
        except BadStatusLine:
            raise ManagerError("Communication with subscription manager interrupted")
        except rhsm_connection.RateLimitExceededException as e:
            raise ManagerThrottleError(e.retry_after)
        except rhsm_connection.GoneException:
            raise ManagerError("Communication with subscription manager failed: consumer no longer exists")
        except rhsm_connection.ConnectionException as e:
            if hasattr(e, 'code'):
                raise ManagerError("Communication with subscription manager failed with code %d: %s" % (e.code, str(e)))
            raise ManagerError("Communication with subscription manager failed: %s" % str(e))

        if is_async is True:
            report.state = AbstractVirtReport.STATE_CREATED
            report.job_id = result['id']
        else:
            report.state = AbstractVirtReport.STATE_FINISHED
        return result 
Example #11
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 #12
Source File: test_states.py    From Tautulli with GNU General Public License v3.0 4 votes vote down vote up
def test_2_KeyboardInterrupt(self):
        # Raise a keyboard interrupt in the HTTP server's main thread.
        # We must start the server in this, the main thread
        engine.start()
        cherrypy.server.start()

        self.persistent = True
        try:
            # Make the first request and assert there's no "Connection: close".
            self.getPage('/')
            self.assertStatus('200 OK')
            self.assertBody('Hello World')
            self.assertNoHeader('Connection')

            cherrypy.server.httpserver.interrupt = KeyboardInterrupt
            engine.block()

            self.assertEqual(db_connection.running, False)
            self.assertEqual(len(db_connection.threads), 0)
            self.assertEqual(engine.state, engine.states.EXITING)
        finally:
            self.persistent = False

        # Raise a keyboard interrupt in a page handler; on multithreaded
        # servers, this should occur in one of the worker threads.
        # This should raise a BadStatusLine error, since the worker
        # thread will just die without writing a response.
        engine.start()
        cherrypy.server.start()
        # From python3.5 a new exception is retuned when the connection
        # ends abruptly:
        #   http.client.RemoteDisconnected
        # RemoteDisconnected is a subclass of:
        #   (ConnectionResetError, http.client.BadStatusLine)
        # and ConnectionResetError is an indirect subclass of:
        #    OSError
        # From python 3.3 an up socket.error is an alias to OSError
        # following PEP-3151, therefore http.client.RemoteDisconnected
        # is considered a socket.error.
        #
        # raise_subcls specifies the classes that are not going
        # to be considered as a socket.error for the retries.
        # Given that RemoteDisconnected is part BadStatusLine
        # we can use the same call for all py3 versions without
        # sideffects. python < 3.5 will raise directly BadStatusLine
        # which is not a subclass for socket.error/OSError.
        try:
            self.getPage('/ctrlc', raise_subcls=BadStatusLine)
        except BadStatusLine:
            pass
        else:
            print(self.body)
            self.fail('AssertionError: BadStatusLine not raised')

        engine.block()
        self.assertEqual(db_connection.running, False)
        self.assertEqual(len(db_connection.threads), 0)