示例#1
0
 def _GetRawManPageText(self):
   """Returns the raw man page text."""
   url = 'http://man7.org/linux/man-pages/man1/{}.1.html'.format(self.command)
   response, content = http.HttpClient().request(url)
   if response.status != 200:
     raise NoManPageTextForCommand(
         'Cannot get URL man page text for [{}].'.format(self.command))
   return content.decode('utf-8')
示例#2
0
def ReportMetrics(metrics_file_path):
    """Sends the specified anonymous usage event to the given analytics endpoint.

  Args:
      metrics_file_path: str, File with pickled metrics (list of tuples).
  """
    with files.BinaryFileReader(metrics_file_path) as metrics_file:
        metrics = pickle.load(metrics_file)
    os.remove(metrics_file_path)

    http_client = http.HttpClient(timeout=TIMEOUT_IN_SEC,
                                  proxy_info=http_proxy.GetHttpProxyInfo())

    for metric in metrics:
        http_client.request(metric[0],
                            method=metric[1],
                            body=metric[2],
                            headers=metric[3])
示例#3
0
    def _SendJsonRequest(self, method, path, body=None, timeout_secs=300):
        """Sends a request to the broker.

    Args:
      method: (str) The HTTP method.
      path: (str) The URI path.
      body: (str) The request body.
      timeout_secs: (float) The request timeout, in seconds.

    Returns:
      (HTTPResponse, str) or (None, None).

    Raises:
      RequestTimeoutError: The request timed-out.
      RequestSocketError: The request failed due to a socket error.
      RequestError: The request errored out in some other way.
    """
        uri = 'http://{0}{1}'.format(self._address, path)
        http_client = http.HttpClient(timeout=timeout_secs)
        try:
            http_response, body = http_client.request(
                uri=uri,
                method=method,
                headers={'Content-Type': 'application/json; charset=UTF-8'},
                body=body)
            return http_response, body.decode('utf-8')
        except socket.error as e:
            if isinstance(e, socket.timeout):
                raise RequestTimeoutError(e)
            error = RequestSocketError(e)
            if e.errno:
                error.errno = e.errno
            raise error
        except six.moves.http_client.HTTPException as e:
            if isinstance(e, six.moves.http_client.ResponseNotReady):
                raise RequestTimeoutError(e)
            raise RequestError(e)
        except httplib2.HttpLib2Error as e:
            raise RequestError(e)