コード例 #1
0
ファイル: builder.py プロジェクト: nzgamer41/gvsbuild
    def urlretrieve(self, url, filename, reporthook, ssl_ignore_cert=False):
        """
        Retrieve a URL into a temporary location on disk.

        Requires a URL argument. If a filename is passed, it is used as
        the temporary file location. The reporthook argument should be
        a callable that accepts a block number, a read size, and the
        total file size of the URL target. The data argument should be
        valid URL encoded data.

        If a filename is passed and the URL points to a local resource,
        the result is a copy from local file to new file.

        Returns a tuple containing the path to the newly created
        data file as well as the resulting HTTPMessage object.
        """
        url_type, path = splittype(url)

        if ssl_ignore_cert:
            # ignore certificate
            ssl_ctx = ssl._create_unverified_context()
        else:
            # let the library does the work
            ssl_ctx = None

        msg = 'Opening %s ...' % (url, )
        print(msg, end='\r')
        with contextlib.closing(urlopen(url, None, context=ssl_ctx)) as fp:
            print('%*s' % (
                len(msg),
                '',
            ), end='\r')
            headers = fp.info()

            with open(filename, 'wb') as tfp:
                result = filename, headers
                bs = 1024 * 8
                size = -1
                read = 0
                blocknum = 0
                if "content-length" in headers:
                    size = int(headers["Content-Length"])

                reporthook(blocknum, bs, size)

                while True:
                    block = fp.read(bs)
                    if not block:
                        break
                    read += len(block)
                    tfp.write(block)
                    blocknum += 1
                    reporthook(blocknum, bs, size)

        if size >= 0 and read < size:
            raise ContentTooShortError(
                "retrieval incomplete: got only %i out of %i bytes" %
                (read, size), result)

        return result
コード例 #2
0
def _urlretrieve_with_opener(opener, url, filename, data=None):
    if not _is_py3:
        raise SyntaxError('Only call this in Python 3 code.')

    # borrowed from the original implementation at urllib.request.urlretrieve.
    with closing(opener.open(url, data=data)) as src:
        headers = src.info()

        with open(filename, 'wb') as dest:
            result = filename, headers
            bs = 1024 * 8
            size = -1
            read = 0
            blocknum = 0
            if "content-length" in headers:
                size = int(headers["Content-Length"])

            while True:
                block = src.read(bs)
                if not block:
                    break
                read += len(block)
                dest.write(block)
                blocknum += 1

    if size >= 0 and read < size:
        raise ContentTooShortError(
            'retrieval incomplete: got only %i out of %i bytes' % (read, size),
            result)

    return result