说明

本代码是仿写镜像库中developer024/networkdownload功能所写
在原基础上提供多个下载链接的支持
可替换变量:
urlList 下载的链接
thread 线程数
goal 需要消耗的流量 0为无限

直接部署使用

1.爱快部署
2.linux部署

代码

import requests
import time
from concurrent.futures import ThreadPoolExecutor
import random
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

urlList = ['https://img.cmvideo.cn/publish/noms/2023/12/06/1O4SHFIFR36BD.gif', 'https://img.cmvideo.cn/publish/noms/2023/12/06/1O4SHFIFR36BD.gif']
thread = 5  #线程数量
goal = 0  #消耗的流量单位GB
if goal > 0:
    goal = goal * 1024 * 1024 * 1024  #GB转为B

wasted = 0  #已消费的流量
runing = 0  #正在运行的数量
#线程池
executor = ThreadPoolExecutor(max_workers=thread)
#下载连接池
session = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_connections=thread, pool_maxsize=thread+1, max_retries=1, pool_block=False)
session.mount('http://', adapter)
session.mount('https://', adapter)


# 下载文件
def download(url):
    try:
        global runing
        runing += 1
        response = session.get(url, stream=True)
        if response.status_code == 200:
            for chunk in response.iter_content(chunk_size=1020):  # 按块读取文件内容
                if goal > 0:  #goal为0时,不记录wasted
                    global wasted
                    wasted += 1020  #消费流量
                    if wasted > goal:
                        logging.info("流量已经消费了 %s B,超过了目标 %s B,终止下载", wasted, goal)
                        return True
    except Exception as e:
        logging.info("下载失败", e)
    finally:
        runing -= 1
    response.close()  # 关闭下载连接
    return True


def startDownload():
    time.sleep(random.randint(1, 10))
    global wasted
    wasted = 0
    # 开始下载
    i = thread
    while i > 0:
        executor.submit(download, random.choice(urlList))
        # 休眠0.01秒-0.1秒
        time.sleep(random.randint(1, 10) / 100)
        i -= 1


if __name__ == "__main__":
    startDownload()
    while True:
        if (goal==0 or wasted < goal) and runing < thread:
            logging.info("补充下载链接数:%s", thread - runing)
            i = thread - runing
            while i> 0:
                executor.submit(download, random.choice(urlList))
                time.sleep(random.randint(1, 10) / 100)
                i-=1
        time.sleep(5)

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐