def _ReadResponse(self, params, connection):
    # Check status
    response = connection.getresponse()
    if response.status != httplib.OK:
      raise RuntimeError('Request failed: %s %s' % (
          response.status, response.reason))
    if response.getheader('Transfer-Encoding') != 'chunked':
      raise RuntimeError('Invalid response headers: %s' % response.msg)
    from_stream = response

    # Map chunk "id" to writable file_pointer ("fp").
    id_to_fp = {}
    id_to_fp['1'] = self._stdout
    id_to_fp['2'] = self._stderr
    id_to_fp['exit'] = StringIO.StringIO()

    # Map chunk "id" to output file_name ("fn").
    id_to_fn = {}
    for index, param in enumerate(params):
      if isinstance(param, OutputFileParameter):
        id_to_fn['o%d' % index] = param.value

    # Read chunks
    try:
      while True:
        header = ChunkHeader()
        header.Parse(from_stream.readline())
        if header.len_ <= 0:
          break
        handler_id = header.id_
        fp = id_to_fp.get(handler_id)
        if not fp and handler_id not in id_to_fn:
          raise ValueError('Unknown output stream id: %s' % header)
        if header.is_absent_ or header.is_empty_:
          ReadExactly(from_stream, header.len_)
        else:
          if not fp:
            fn = id_to_fn[handler_id]
            # This fn path is from our caller, not the server, so we trust it
            if header.is_tar_:
              fp = Untar(fn)
            elif os.path.isfile(fn) or not os.path.exists(fn):
              fp = open(fn, 'wb')
            else:
              raise ValueError('Expecting a tar, not %s' % header)
            id_to_fp[handler_id] = fp
          bytes_read = 0
          while bytes_read < header.len_:
            data = from_stream.read(min(MAX_READ, header.len_ - bytes_read))
            bytes_read += len(data)
            fp.write(data)
        if ReadExactly(from_stream, 2) != '\r\n':
          raise ValueError('Chunk does not end with crlf')
    finally:
      for handler_id in id_to_fn:
        if handler_id in id_to_fp:
          id_to_fp[handler_id].close()

    errcode_stream = id_to_fp['exit']
    return int(errcode_stream.getvalue()) if errcode_stream.tell() else None
Reads the response chunks from the server.
dataset/ETHPy150Open google/lab_device_proxy/lab_device_proxy_client.py/LabDeviceProxyClient._ReadResponse