python脚本刷下行流量
本文最后更新于 2024-08-22,
若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益, 请联系我 删除。
本站只有Telegram群组为唯一交流群组, 点击加入
文章内容有误?申请成为本站文章修订者或作者? 向站长提出申请
说明
本代码是仿写Docker镜像库中developer024/networkdownload功能所写
在原基础上提供多个下载链接的支持
可替换变量:
urlList 下载的链接
thread 线程数
goal 需要消耗的流量 0为无限
脚本代码
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)
评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果