码迷,mamicode.com
首页 > 编程语言 > 详细

python3 retrieve()

时间:2020-03-31 14:22:47      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:ESS   efault   self   hook   def   class   flush   cal   repo   

retreive()被认为是个老旧函数,可能会在将来抛弃。
urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)
Exceptions和函数urlopen()相同。
具体文档,参见https://docs.python.org/3/library/urllib.request.html#module-urllib.request

代码片段

from urllib.request import urlretrieve
import socket
import os
import sys

def  reporthook(blocknum, bs, size):
    #  blocknum:已经下载的数据块       bs:数据块的大小      size:远程文件的大小
    per = 100.0 * blocknum * bs / size
    if per > 100 :
        per = 100
    sys.stdout.write(‘Download progress: %.2f%%  \r‘%per)
    sys.stdout.flush()
class ClassName():
    def __init__(self):
        socket.setdefaulttimeout(30)

        def run(self):
               ...
           while True:
                .....
                try:
                    urlretrieve(url,localfile, reporthook= reporthook) 
                except socket.timeout:
                    self.redownload(url,localfile) 
                except Exception as e:
                    StatusCode=e.getcode()
                    if StatusCode == 404:
                        print(e)
                        break
                    self.redownload(url,localfile)

    def redownload(self,url,localfile):
        count = 1
        while count <= 5:
            try:
                urlretrieve(url,localfile, reporthook= reporthook)                                                
                break
            except socket.timeout:
                err_info = ‘Reloading for %d time.....‘%count if count == 1 else ‘Reloading for %d times.....‘%count
                print(err_info)
                count += 1
        if count > 5:
            print("download job failed!") 

python3 retrieve()

标签:ESS   efault   self   hook   def   class   flush   cal   repo   

原文地址:https://blog.51cto.com/whbill/2483367

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!