Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用urllib2重用请求模块来拆分范围标头_Python_Urllib2_Python Requests_Multipart_Http Range - Fatal编程技术网

Python 如何使用urllib2重用请求模块来拆分范围标头

Python 如何使用urllib2重用请求模块来拆分范围标头,python,urllib2,python-requests,multipart,http-range,Python,Urllib2,Python Requests,Multipart,Http Range,我有一些未经分解的代码,其中如果在单个块中请求下载文件,我将使用requests模块进行下载,但由于我不知道如何在请求时将块拆分为多个范围(本例中为irange),使用多个线程如何使用requests模块实现相同的功能 def _grabAndWriteToDisk(url, saveTo, first=None, queue=None, mode='wb', irange=None): """ Function to download file using requests when

我有一些未经分解的代码,其中如果在单个块中请求下载文件,我将使用requests模块进行下载,但由于我不知道如何在请求时将块拆分为多个范围(本例中为irange),使用多个线程如何使用requests模块实现相同的功能

def _grabAndWriteToDisk(url, saveTo, first=None, queue=None, mode='wb', irange=None):
    """ Function to download file using requests when not multipart range,
        else uses urllib2 when multi threaded multi part file download requests.

        Args:
            url(str): url of file to download
            saveTo(str): path where to save file
            first(int): starting byte of the range
            queue(Queue.Queue): queue object to set status for file download
            mode(str): mode of file to be downloaded
            irange(str): range of byte to download

    """
    fileName = url.split('/')[-1]
    filePath = os.path.join(saveTo, fileName)
    fileSize = int(_fdUtils.getUrlSizeInBytes(url))
    downloadedFileSize = 0 if not first else first
    block_sz = 8192

    def statusSet(que, dlfs, fileSize, fName, savTo):
        STOP_REQUEST.set()
        if que:
            que.task_done()
        _log.info("Download Completed %s%% for file %s, saved to %s",
            dlfs * 100. / fileSize, fName, savTo)

    if not irange:
        resp = requests.get(url, stream=True)
        for fileBuffer in resp.iter_content(block_sz):
            if not fileBuffer:
                break

            with open(filePath, mode) as fd:
                downloadedFileSize += len(fileBuffer)
                fd.write(fileBuffer)
                status = r"%10d  [%3.2f%%]" % (downloadedFileSize, downloadedFileSize * 100. / fileSize)
                status = status + chr(8)*(len(status)+1)
                sys.stdout.write('%s\r' % status)
                time.sleep(.05)
                sys.stdout.flush()
                if downloadedFileSize == fileSize:
                    statusSet(queue, downloadedFileSize, fileSize, fileName, saveTo)

    else:
        req = urllib2.Request(url)
        req.headers['Range'] = 'bytes=%s' % irange

        urlFh = urllib2.urlopen(req)
        with open(filePath, mode) as fh:
            while not STOP_REQUEST.isSet():
                fileBuffer = urlFh.read(block_sz)
                if not fileBuffer:
                    break
                downloadedFileSize += len(fileBuffer)
                fh.write(fileBuffer)

                status = r"%10d  [%3.2f%%]" % (downloadedFileSize, downloadedFileSize * 100. / fileSize)
                status = status + chr(8)*(len(status)+1)
                sys.stdout.write('%s\r' % status)
                time.sleep(.05)
                sys.stdout.flush()
                if downloadedFileSize == fileSize:
                    statusSet(queue, downloadedFileSize, fileSize, fileName, saveTo)

如何使用urllib2重用请求模块来拆分范围标头?

您正在努力解决的问题是什么?看起来您需要做的就是将
范围
标题添加到
请求.get()
调用中,该调用恰好接受
标题
参数(使其成为字典)。@MartijnPieters您的意思是使用
响应标题['content-length']
?其中resp是
resp=requests.get(url,stream=True)
No,我的意思是你可以用
requests.get(url,headers={'Range':'bytes=%s'%irange},stream=True)
。啊哈。因此,这意味着我可以在以后设置它,比如
resp.headers['Range']=“bytes=%s”%irange
No,您的原始代码会在请求上设置头,更改返回响应上的头毫无意义。