コード例 #1
0
 def __ProcessResponse(self, response):
     """Process this response (by updating self and writing to self.stream)."""
     if response.status_code not in self._ACCEPTABLE_STATUSES:
         raise exceptions.TransferRetryError(response.content)
     if response.status_code in (httplib.OK, httplib.PARTIAL_CONTENT):
         self.stream.write(response.content)
         self.__progress += response.length
         if response.info and 'content-encoding' in response.info:
             # TODO: Handle the case where this changes over a download.
             self.__encoding = response.info['content-encoding']
     elif response.status_code == httplib.NO_CONTENT:
         # It's important to write something to the stream for the case
         # of a 0-byte download to a file, as otherwise python won't
         # create the file.
         self.stream.write('')
     return response
コード例 #2
0
    def GetRange(self, start, end=None, additional_headers=None):
        """Retrieve a given byte range from this download, inclusive.

    Range must be of one of these three forms:
    * 0 <= start, end = None: Fetch from start to the end of the file.
    * 0 <= start <= end: Fetch the bytes from start to end.
    * start < 0, end = None: Fetch the last -start bytes of the file.

    (These variations correspond to those described in the HTTP 1.1
    protocol for range headers in RFC 2616, sec. 14.35.1.)

    Args:
      start: (int) Where to start fetching bytes. (See above.)
      end: (int, optional) Where to stop fetching bytes. (See above.)
      additional_headers: (bool, optional) Any additional headers to
          pass with the request.

    Returns:
      None. Streams bytes into self.stream.
    """
        self.EnsureInitialized()
        progress_end_normalized = False
        if self.total_size is not None:
            progress, end = self.__NormalizeStartEnd(start, end)
            progress_end_normalized = True
        else:
            progress = start
        while not progress_end_normalized or progress < end:
            response = self.__GetChunk(progress,
                                       end=end,
                                       additional_headers=additional_headers)
            if not progress_end_normalized:
                self.__SetTotal(response.info)
                progress, end = self.__NormalizeStartEnd(start, end)
                progress_end_normalized = True
            response = self.__ProcessResponse(response)
            progress += response.length
            if not response:
                raise exceptions.TransferRetryError(
                    'Zero bytes unexpectedly returned in download response')