【问题标题】:using python stream downloading file with server limit使用具有服务器限制的python流下载文件
【发布时间】:2015-05-04 17:33:26
【问题描述】:

我尝试使用python从服务器下载文件,有时文件非常大,我想要一些进度条,我可以想出的一种方法是在流中下载,这样我可以打印进度。目前我已经尝试了标准的 urlopen、urlretrieve 和 requests 模块(开启了流)。

很明显,urlopen 不能下载流中的文件,请求模块支持这个,但是服务器对我一次可以下载的文件有限制(它的限制是 1)。所以每次,我尝试使用请求,它只会让网页告诉我等待,还有其他方法吗?

【问题讨论】:

    标签: python web-crawler python-requests


    【解决方案1】:

    我最近使用此功能下载了许多类型的媒体:

    import sys
    import requests
    import time
    def download_resource(domain, url, file_name = None, download = True):
        cookies = {}
        s = requests.Session()
    
        s.config['keep_alive'] = True
        #add your own cookies here, I have a specific function I call
        #for my application but yours is different
        r = s.get(url, cookies = cookies, stream = True)
    
        if not r.ok:
            print "error in downloading"
            return -1
    
        file_size = int(r.headers['content-length']) 
    
        if not file_name:
            try:
                temp = r.headers['content-disposition']
            except Exception as e:
                pass
                #failing download
                return -1
            else:
                if not temp:
                    return -1
                else:
                    file_name = temp.split("filename=")[-1]
                    return_obj["filename"] = file_name
        #print "File size:", file_size
        #print "\n", str(self.entire_size / float(1024*1024*1024)), "\n"
        print "Downloading:", file_name
        if download:
            with open(file_name, "wb") as fh:
                count = 1
                chunk_size = 1048576
                start_time = time.time()
                try:
                    for block in r.iter_content(chunk_size):
                        total_time = time.time() - start_time
                        percent = count*chunk_size/float(file_size) * 100.0
                        fraction = int(percent/5)
    
                        download_speed = 1.0 / total_time
    
                        sys.stdout.write('\r')
                        sys.stdout.write("[%-20s] %d%%  %3.2f MB/s       " % ('='* fraction , percent, download_speed))
                        sys.stdout.flush()
                        if not block:
                            break
                        fh.write(block)
                        count += 1
                        start_time = time.time()
                except Exception as e:
                    print e
                finally:    
                    #close up the stream
                    r.close()
    

    【讨论】:

    • 我做了类似的事情,但是只收到一个网页说我需要等待几秒钟才能下载下一个文件,几秒钟后又说同样的事情。我猜这是因为服务器同时限制了一个连接。如果我让stream=False,这不会发生。有什么想法吗?
    • 您是否在请求中使用了会话对象?据我了解,您所说的是网络服务器将单个客户端的并发连接数限制为一个。如果您不使用会话对象,我会尝试这样做,并设置 keep-alive。
    • 我确实使用过 session,但是我关闭了 keep-alive 并忘记打开它。我要再试一次。谢谢!
    • 现在可以工作了,但还是有问题,请参阅 [link] (stackoverflow.com/questions/30056960/…)
    猜你喜欢
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    相关资源
    最近更新 更多