《疯狂讲义》之多线程下载文件类

该源码为疯狂讲义多线程下载工具类, 可以使用,如果需要正式投产 建议添加相关异常处理. 

from urllib.request import *
import threading


class DownUtil:
    def __init__(self, path, targetfile, thread_num):
        # need down file path
        self.path = path
        # Thread number
        self.thread_num = thread_num
        # save file path
        self.targetfile = targetfile
        # Threads
        self.threads = []
        self.file_size = 0;

    def download(self):
        # create request obj
        req = Request(url=self.path,method='GET')
        req.add_header('Accept','*/*')
        req.add_header('CHarset','UTF-8')
        req.add_header('Connection', 'Keep-Alive')
        # open the source
        f = urlopen(req)
        # get file size
        self.file_size = int(dict(f.headers).get('Content-Length',0))
        f.close()

        # current thread need down file size
        current_part_size =self.file_size // self.thread_num + 1
        for i in range(self.thread_num):
            # record begin down file pos
            start_pos = i* current_part_size
            t = open(self.targetfile, 'wb')
            t.seek(start_pos,0)
            td = DownThread(self.path, start_pos, current_part_size, t)
            self.threads.append(td)
            # start Thread
            td.start()

    def get_complete_rate(self):
        sum_size = 0
        for i in range(self.thread_num):
            sum_size += self.threads[i].length
        return sum_size/self.file_size


class DownThread(threading.Thread):
    def __init__(self, path, start_pos, current_part_size, current_part):
        super().__init__()
        self.path = path
        # current thread down pos
        self.start_pos = start_pos
        # need down file size
        self.current_part_size = current_part_size
        # has downed file length
        self.length =0
        self.current_part = current_part

    def run(self):
        req = Request(url=self.path,method='GET')
        req = Request(url=self.path, method='GET')
        req.add_header('Accept', '*/*')
        req.add_header('CHarset', 'UTF-8')
        req.add_header('Connection', 'Keep-Alive')
        # open the source
        f = urlopen(req)
        # skip pos
        for i in range(self.start_pos):
            f.read(1)

        while self.length < self.current_part_size:
            data = f.read(1024)
            if data is None or len(data) <=0:
                break

            self.current_part.write(data)
            self.length += len(data)

        self.current_part.close()
        f.close()


if __name__ =="__main__":
    urlfile ="http://pic1.win4000.com/wallpaper/b/57e8cefd76fdc.jpg"
    down = DownUtil(urlfile,"a.jpg",3)
    down.download()

    def show_process():
        print('finished %.2f' % down.get_complete_rate())
        global thTimer
        if down.get_complete_rate() < 1 :
            thTimer = threading.Timer(0.1,show_process)
            thTimer.start()


    thTimer = threading.Timer(0.1,show_process)
    thTimer.start()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值