python3取得()

断片

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)

while True:
                .....
                try:
                    urlretrieve(url1,localfile, reporthook= reporthook) 
                except socket.timeout:
                    self.redownload(url1,localfile) 
                except Exception as e:
                    StatusCode=e.getcode()
                    if StatusCode == 404:
                        print(e)
                        break
                    self.redownload(url1,localfile)
                i += 1

    def redownload(self,url1,localfile):
        count = 1
        while count <= 5:
            try:
                urlretrieve(url1,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!") 

おすすめ

転載: blog.51cto.com/whbill/2483367