我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用six.moves.http_client.HTTPException()。
def _refresh(self, http): """Refreshes the access token. Skip all the storage hoops and just refresh using the API. Args: http: an object to be used to make HTTP requests. Raises: HttpAccessTokenRefreshError: When the refresh fails. """ try: self._retrieve_info(http) self.access_token, self.token_expiry = _metadata.get_token( http, service_account=self.service_account_email) except http_client.HTTPException as err: raise client.HttpAccessTokenRefreshError(str(err))
def __call__(self, path, key_file=None, cert_file=None, **kwargs): # Only pass in the named arguments that HTTPConnection constructor # understands tmpKwargs = {} for key in http_client.HTTPConnection.__init__.__code__.co_varnames: if key in kwargs and key != 'self': tmpKwargs[key] = kwargs[key] tunnel = http_client.HTTPConnection(path, **tmpKwargs) tunnel.request('CONNECT', self.proxyPath) resp = tunnel.getresponse() if resp.status != 200: raise http_client.HTTPException("{0} {1}".format(resp.status, resp.reason)) retval = http_client.HTTPSConnection(path) retval.sock = _SocketWrapper(tunnel.sock, keyfile=key_file, certfile=cert_file) return retval
def get(http, path, root=METADATA_ROOT, recursive=None): """Fetch a resource from the metadata server. Args: http: an object to be used to make HTTP requests. path: A string indicating the resource to retrieve. For example, 'instance/service-accounts/default' root: A string indicating the full path to the metadata server root. recursive: A boolean indicating whether to do a recursive query of metadata. See https://cloud.google.com/compute/docs/metadata#aggcontents Returns: A dictionary if the metadata server returns JSON, otherwise a string. Raises: http_client.HTTPException if an error corrured while retrieving metadata. """ url = urlparse.urljoin(root, path) url = _helpers._add_query_parameter(url, 'recursive', recursive) response, content = transport.request( http, url, headers=METADATA_HEADERS) if response.status == http_client.OK: decoded = _helpers._from_bytes(content) if response['content-type'] == 'application/json': return json.loads(decoded) else: return decoded else: raise http_client.HTTPException( 'Failed to retrieve {0} from the Google Compute Engine' 'metadata service. Response:\n{1}'.format(url, response))
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))
def test_get_failure(self): http = request_mock( http_client.NOT_FOUND, 'text/html', '<p>Error</p>') with self.assertRaises(http_client.HTTPException): _metadata.get(http, PATH) # Verify mocks. self.assertEqual(http.requests, 1) self.assertEqual(http.uri, EXPECTED_URL) self.assertEqual(http.method, 'GET') self.assertIsNone(http.body) self.assertEqual(http.headers, _metadata.METADATA_HEADERS)
def get(http, path, root=METADATA_ROOT, recursive=None): """Fetch a resource from the metadata server. Args: http: an object to be used to make HTTP requests. path: A string indicating the resource to retrieve. For example, 'instance/service-accounts/defualt' root: A string indicating the full path to the metadata server root. recursive: A boolean indicating whether to do a recursive query of metadata. See https://cloud.google.com/compute/docs/metadata#aggcontents Returns: A dictionary if the metadata server returns JSON, otherwise a string. Raises: http_client.HTTPException if an error corrured while retrieving metadata. """ url = urlparse.urljoin(root, path) url = _helpers._add_query_parameter(url, 'recursive', recursive) response, content = transport.request( http, url, headers=METADATA_HEADERS) if response.status == http_client.OK: decoded = _helpers._from_bytes(content) if response['content-type'] == 'application/json': return json.loads(decoded) else: return decoded else: raise http_client.HTTPException( 'Failed to retrieve {0} from the Google Compute Engine' 'metadata service. Response:\n{1}'.format(url, response))
def test_ssl_tunnel_http_failure(self): from six.moves import http_client def should_fail(): connect.SoapStubAdapter('vcsa', 80, httpProxyHost='vcsa').GetConnection() self.assertRaises(http_client.HTTPException, should_fail)
def InvokeMethod(self, mo, info, args): # This retry logic is replicated in InvokeAccessor and the two copies need # to be in sync retriesLeft = self.retryCount while retriesLeft > 0: try: if self.state == self.STATE_UNAUTHENTICATED: self._CallLoginMethod() # Invoke the method status, obj = self.soapStub.InvokeMethod(mo, info, args, self) except (socket.error, http_client.HTTPException, ExpatError): if self.retryDelay and retriesLeft: time.sleep(self.retryDelay) retriesLeft -= 1 continue if status == 200: # Normal return from the server, return the object to the caller. return obj # An exceptional return from the server if isinstance(obj, self.SESSION_EXCEPTIONS): # Our session might've timed out, change our state and retry. self._SetStateUnauthenticated() else: # It's an exception from the method that was called, send it up. raise obj # Raise any socket/httplib errors caught above. raise SystemError() ## Retrieve a managed property # # @param self self # @param mo managed object # @param info property info
def InvokeAccessor(self, mo, info): # This retry logic is replicated in InvokeMethod and the two copies need # to be in sync retriesLeft = self.retryCount while retriesLeft > 0: try: if self.state == self.STATE_UNAUTHENTICATED: self._CallLoginMethod() # Invoke the method obj = StubAdapterBase.InvokeAccessor(self, mo, info) except (socket.error, http_client.HTTPException, ExpatError): if self.retryDelay and retriesLeft: time.sleep(self.retryDelay) retriesLeft -= 1 continue except Exception as e: if isinstance(e, self.SESSION_EXCEPTIONS): # Our session might've timed out, change our state and retry. self._SetStateUnauthenticated() else: raise e return obj # Raise any socket/httplib errors caught above. raise SystemError() ## Handle the login method call # # This method calls the login method on the soap stub and changes the state # to authenticated
def _download_tarball_and_verify(request, staging_path): # NOTE(johngarbutt) By default, there is no timeout. # To ensure the script does not hang if we lose connection # to glance, we add this socket timeout. # This is here so there is no chance the timeout out has # been adjusted by other library calls. socket.setdefaulttimeout(SOCKET_TIMEOUT_SECONDS) try: response = urllib2.urlopen(request) except urllib2.HTTPError, error: # noqa raise RetryableError(error) except urllib2.URLError, error: # noqa raise RetryableError(error) except httplib.HTTPException, error: # noqa # httplib.HTTPException and derivatives (BadStatusLine in particular) # don't have a useful __repr__ or __str__ raise RetryableError('%s: %s' % (error.__class__.__name__, error)) url = request.get_full_url() logging.info("Reading image data from %s" % url) callback_data = {'bytes_read': 0} checksum = md5.new() def update_md5(chunk): callback_data['bytes_read'] += len(chunk) checksum.update(chunk) try: try: utils.extract_tarball(response, staging_path, callback=update_md5) except Exception, error: # noqa raise RetryableError(error) finally: bytes_read = callback_data['bytes_read'] logging.info("Read %d bytes from %s", bytes_read, url) # Use ETag if available, otherwise content-md5(v2) or # X-Image-Meta-Checksum(v1) etag = response.info().getheader('etag', None) if etag is None: etag = response.info().getheader('content-md5', None) if etag is None: etag = response.info().getheader('x-image-meta-checksum', None) # Verify checksum using ETag checksum = checksum.hexdigest() if etag is None: msg = "No ETag found for comparison to checksum %(checksum)s" logging.info(msg % {'checksum': checksum}) elif checksum != etag: msg = 'ETag %(etag)s does not match computed md5sum %(checksum)s' raise RetryableError(msg % {'checksum': checksum, 'etag': etag}) else: msg = "Verified image checksum %(checksum)s" logging.info(msg % {'checksum': checksum})
def __call__(self, url, method='GET', body=None, headers=None, timeout=None, **kwargs): """Make an HTTP request using http.client. Args: url (str): The URI to be requested. method (str): The HTTP method to use for the request. Defaults to 'GET'. body (bytes): The payload / body in HTTP request. headers (Mapping): Request headers. timeout (Optional(int)): The number of seconds to wait for a response from the server. If not specified or if None, the socket global default timeout will be used. kwargs: Additional arguments passed throught to the underlying :meth:`~http.client.HTTPConnection.request` method. Returns: Response: The HTTP response. Raises: google.auth.exceptions.TransportError: If any exception occurred. """ # socket._GLOBAL_DEFAULT_TIMEOUT is the default in http.client. if timeout is None: timeout = socket._GLOBAL_DEFAULT_TIMEOUT # http.client doesn't allow None as the headers argument. if headers is None: headers = {} # http.client needs the host and path parts specified separately. parts = urllib.parse.urlsplit(url) path = urllib.parse.urlunsplit( ('', '', parts.path, parts.query, parts.fragment)) if parts.scheme != 'http': raise exceptions.TransportError( 'http.client transport only supports the http scheme, {}' 'was specified'.format(parts.scheme)) connection = http_client.HTTPConnection(parts.netloc, timeout=timeout) try: _LOGGER.debug('Making request: %s %s', method, url) connection.request( method, path, body=body, headers=headers, **kwargs) response = connection.getresponse() return Response(response) except (http_client.HTTPException, socket.error) as caught_exc: new_exc = exceptions.TransportError(caught_exc) six.raise_from(new_exc, caught_exc) finally: connection.close()