zoukankan      html  css  js  c++  java
  • python多线程下载ts文件

     1 # -*- coding: utf-8 -*-
     2 """
     3 Created on Wed Aug 22 15:56:19 2018
     4 
     5 @author: Administrator
     6 """
     7 
     8 # 在python3下测试
     9 import requests
    10 import threading
    11 import datetime
    12 
    13  
    14 count =0;
    15 def Handler(start, end, url, filename):
    16     
    17     #headers = {'Range': 'bytes=%d-%d' % (start, end-1)}
    18     #r = requests.get(url, headers=headers, stream=True)
    19     for i in filename[start:end]:
    20         global count
    21         r = requests.get("https://youku.cdn2-youku.com/20180701/12972_72ec5f94/1000k/hls/"+i.replace("
    ",""),stream=True)
    22         #r = requests.get(url) 
    23         with open("ts/"+i.replace("
    ",""), "wb") as code:
    24              code.write(r.content)
    25         count =count+1
    26         print("下载进度:%.2f" % (count/len(filename)))
    27  
    28 def download_file(url, num_thread = 100):
    29     f = open('index.m3u8', 'r', encoding='utf-8')
    30     text_list = f.readlines()
    31     s_list = []
    32     for i in text_list:
    33         if i.find('#EX')==-1:
    34             s_list.append(i)
    35             
    36     f.close()
    37     file_size = len(s_list)
    38  
    39     # 启动多线程写文件
    40     part = file_size // num_thread  # 如果不能整除,最后一块应该多几个字节
    41     for i in range(num_thread):
    42         start = part * i
    43         if i == num_thread - 1:   # 最后一块
    44             end = file_size
    45         else:
    46             end = start + part
    47  
    48         t = threading.Thread(target=Handler, kwargs={'start': start, 'end': end, 'url': url, 'filename': s_list})
    49         t.setDaemon(True)
    50         t.start()
    51  
    52     # 等待所有线程下载完成
    53     main_thread = threading.current_thread()
    54     for t in threading.enumerate():
    55         if t is main_thread:
    56             continue
    57         t.join()
    58     #print('%s 下载完成' % file_name)
    59  
    60 if __name__ == '__main__':
    61     url ="https://youku.cdn2-youku.com/20180701/12972_72ec5f94/1000k/hls/";
    62     start = datetime.datetime.now().replace(microsecond=0)  
    63     download_file(url)
    64     end = datetime.datetime.now().replace(microsecond=0)
    65     print("用时: ", end='')
    66     print(end-start)
  • 相关阅读:
    三年Android开发经验,挥泪整理字节跳动、微软中国凉经,你不看看吗?
    App怎么做才能永不崩溃
    做了八年的Android开发,谁不是一边崩溃,一边默默坚守!
    阿里员工年年绩效A,晒出收入后感叹:996虽然痛苦,发钱时候真香
    2021阅读书单
    不动产测绘概念
    Elasticsearch 集成
    Elasticsearch 环境
    Elasticsearch 优化
    Elasticsearch入门
  • 原文地址:https://www.cnblogs.com/chenlove/p/9518612.html
Copyright © 2011-2022 走看看