Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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返回url文件的流_Python_File Management - Fatal编程技术网

使用python urllib2返回url文件的流

使用python urllib2返回url文件的流,python,file-management,Python,File Management,我有一段Python代码,它得到了一个正确的url 从Amazon S3获得权限并将其复制到本地目录文件: def fileDownload(self, some_Id, localDir, name, range=None): # [...] something happens here # get the Amazon URL fileUrl = we_get_the_amazon_url_here req = urllib2.urlopen(fileUrl

我有一段Python代码,它得到了一个正确的url 从Amazon S3获得权限并将其复制到本地目录文件:

def fileDownload(self, some_Id, localDir, name, range=None):
    # [...] something happens here
    # get the Amazon URL 
    fileUrl = we_get_the_amazon_url_here
    req = urllib2.urlopen(fileUrl)
    if len(range):
        req.headers['Range']='bytes=%s-%s' % (range[0], range[1])

    # Do the download
    with open(os.path.join(localDir,name), 'wb') as fp:
        shutil.copyfileobj(req, fp)
    return 1
我不熟悉urllib2,但我想做的是 这个
fileDownload
方法进入
fileStreaming
方法,以便 可以通过管道将文件的结果内容传送到下游的工具中


你知道怎么做吗?

我想你应该阅读一些文档。 但是,您的“req”对象类似于文件,所以您可以使用read()方法获取其内容

def fileDownload(self, some_Id, range=None):
    # [...] something happens here
    # get the Amazon URL 
    fileUrl = we_get_the_amazon_url_here
    req = urllib2.urlopen(fileUrl)
    if len(range):
        req.headers['Range']='bytes=%s-%s' % (range[0], range[1])

    return req.read()