【问题标题】:return stream of url file with python urllib2使用 python urllib2 返回 url 文件流
【发布时间】:2013-12-11 10:53:21
【问题描述】:

我有一段 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方法,这样我 可以将文件的结果内容通过管道传输到下游的工具中。

任何想法如何做到这一点?

【问题讨论】:

    标签: python file-management


    【解决方案1】:

    我认为您应该阅读一些文档。 但是,您的“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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      相关资源
      最近更新 更多