zoukankan      html  css  js  c++  java
  • 06-python进阶-多线程下载器练手

    我们需要用python 写一个多线程的下载器

    我们要先获取这个文件的大小 然后将其分片 然后启动多线程 分别去下载 然后将其拼接起来

    #!/usr/bin/env python
    #coding:utf-8
    import requests

    from multiprocessing.dummy import Pool as ThreadPool

    class my_download(object):

    def __init__(self,URL):
    self.url = URL
    self.thread_num = 5
    self.file_nume = URL.split('/')[-1]
    url_headers = requests.head(self.url)
    self.total = int(url_headers.headers['Content-Length'])
    print self.total
    #print type('total is %s' % (self.total))

    def get_file_range(self):
    ranges = []

    download_num = int(self.total/self.thread_num)
    print download_num
    for i in range(self.thread_num):
    if i == self.thread_num-1:
    ranges.append((download_num*i,''))
    else:
    ranges.append((download_num*i,download_num*(i+1)))
    return ranges


    def download_file(self,val):
    f = open(self.file_nume, 'w')
    r = requests.get(self.url, headers={'Range': 'Bytes=%s-%s' % val, 'Accept-Encoding': '*'})
    f.seek(val[0])
    f.write(r.content)
    return f

    def run(self):

    file_list = self.get_file_range()
    #print file_list
    Pool = ThreadPool(4)
    res = map(self.download_file,file_list)

    for i in res:
    i.close()

    Pool.close()
    Pool.join()


    url = "https://img3.doubanio.com/icon/up121443072-1.jpg"
    downloader = my_download(url)
    downloader.run()
  • 相关阅读:
    【LeetCode】链表 linked list(共34题)
    Construct Binary Tree from Preorder and Inorder Traversal<leetcode>
    Triangle <leetcode>
    Jump Game <leetcode>
    C++学习笔记 <const限定词>
    Search for a Range <leetcode>
    Subsets <leetcode>
    各种排序算法总结
    Unique Paths II <leetcode>
    C++学习笔记 <hash_map> <散列映射>
  • 原文地址:https://www.cnblogs.com/nerdlerss/p/7250802.html
Copyright © 2011-2022 走看看