使用帮助
root@debian:/home/data/git/gitee/openwrt/tools# python3 DownloadFileTools.py -h
usage: 流量消失工具-Linux [-h] [-p PATH] [-t TXT] [-mini MINI] [-max MAX] [-ef]
当前脚本版本: 1.0
options:
-h, --help show this help message and exit
-p PATH, --path PATH 设置下载存放路径,默认: /root
-t TXT, --txt TXT 设置URL清单文件(一行一个URL),默认: None
-mini MINI 设置任务间隔时间随机最小数(单位: 秒),默认: 100
-max MAX 设置任务间隔时间随机最大数(单位: 秒),默认: 1000
-ef 进程检测是否使用-ef参数(OpenWrt不需要该参数,主流Linux需要)
root@debian:/home/data/git/gitee/openwrt/tools#
效果
进程检测
root@debian:/home/data/git/gitee/openwrt/tools# python3 DownloadFileTools.py -p /html/ -ef
2023-10-18 13:50:24.133 | DEBUG | __main__:<module>:100 - True
root 2150418 2150413 0 13:50 pts/2 00:00:00 sh -c wget -c https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso
root 2150419 2150418 13 13:50 pts/2 00:00:01 wget -c https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso
2023-10-18 13:50:24.150 | WARNING | __main__:download:62 - 已存在wget任务,本次取消...
root 2150418 2150413 0 13:50 pts/2 00:00:00 sh -c wget -c https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso
root 2150419 2150418 13 13:50 pts/2 00:00:01 wget -c https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso
2023-10-18 13:50:24.168 | WARNING | __main__:download:62 - 已存在wget任务,本次取消...
root 2150418 2150413 0 13:50 pts/2 00:00:00 sh -c wget -c https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso
root 2150419 2150418 13 13:50 pts/2 00:00:01 wget -c https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso
2023-10-18 13:50:24.186 | WARNING | __main__:download:62 - 已存在wget任务,本次取消...
root 2150418 2150413 0 13:50 pts/2 00:00:00 sh -c wget -c https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso
root 2150419 2150418 13 13:50 pts/2 00:00:01 wget -c https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso
2023-10-18 13:50:24.204 | WARNING | __main__:download:62 - 已存在wget任务,本次取消...
root 2150418 2150413 0 13:50 pts/2 00:00:00 sh -c wget -c https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso
root 2150419 2150418 13 13:50 pts/2 00:00:01 wget -c https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso
2023-10-18 13:50:24.222 | WARNING | __main__:download:62 - 已存在wget任务,本次取消...
root 2150418 2150413 0 13:50 pts/2 00:00:00 sh -c wget -c https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso
root 2150419 2150418 13 13:50 pts/2 00:00:01 wget -c https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso
2023-10-18 13:50:24.239 | WARNING | __main__:download:62 - 已存在wget任务,本次取消...
root@debian:/home/data/git/gitee/openwrt/tools#
下载
root@debian:/home/data/git/gitee/openwrt/tools# python3 DownloadFileTools.py -p /html/
2023-10-18 13:50:14.677 | DEBUG | __main__:<module>:100 - False
2023-10-18 13:50:14.691 | DEBUG | __main__:download:65 - 正在下载:https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso ---> deepin-desktop-community-23-Beta2-amd64.iso
--2023-10-18 13:50:14-- https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 2409:8c54:1831:fe00:3::3e9, 2409:8c54:1831:fe00:3::3ea, 221.178.86.248, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|2409:8c54:1831:fe00:3::3e9|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 4501731328 (4.2G), 1483354640 (1.4G) remaining [application/x-cd-image]
Saving to: ‘deepin-desktop-community-23-Beta2-amd64.iso’
deepin-desktop-community-23-Beta2-amd64.is 100%[+++++++++++++++++++++++++++++++++++++++++++++++++++++++++============================>] 4.19G 19.0MB/s in 76s
2023-10-18 13:51:31 (18.6 MB/s) - ‘deepin-desktop-community-23-Beta2-amd64.iso’ saved [4501731328/4501731328]
2023-10-18 13:51:31.265 | INFO | __main__:download:67 - 下载成功,正在删除: deepin-desktop-community-23-Beta2-amd64.iso
2023-10-18 13:51:31.589 | DEBUG | __main__:download:72 - 本次延迟: 413 秒
源码
from argparse import ArgumentParser
from os import system, path, getenv, chdir
from loguru import logger
from time import sleep
from random import randint
class Download:
def __init__(self, txt):
"""
:param txt: URL清单文本文件路径
"""
self.txt = txt
self.url_list = []
def init_read_txt(self):
if self.txt is None:
return
if path.isfile(self.txt):
try:
with open(file=self.txt, encoding="utf8", mode="r") as r:
txt = r.read()
ul = str(txt).split("\n")
if len(ul) >= 1:
self.url_list = ul
except Exception as e:
logger.error(str(e))
else:
logger.warning(f"找不到URL清单文件,将使用程序内置下载清单")
def init_config(self):
"""
初始化变量
:return:
"""
self.url_list.append(
"https://mirrors.aliyun.com/deepin-cd/23-Beta2/deepin-desktop-community-23-Beta2-amd64.iso")
self.url_list.append(
"https://mirrors.aliyun.com/centos/8-stream/isos/aarch64/CentOS-Stream-8-20231009.1-aarch64-boot.iso")
self.url_list.append("https://mirrors.aliyun.com/ubuntu-releases/mantic/ubuntu-23.10.1-desktop-amd64.iso")
self.url_list.append("https://mirrors.aliyun.com/archlinux/iso/2023.10.14/archlinux-2023.10.14-x86_64.iso")
self.url_list.append(
"https://mirrors.tuna.tsinghua.edu.cn/kali-images/current/kali-linux-2023.3-installer-amd64.iso")
self.url_list.append(
"https://mirrors.tuna.tsinghua.edu.cn/nixos-images/nixos-23.05/latest-nixos-plasma5-x86_64-linux.iso")
def download(self):
"""
开始下载
:return:
"""
if len(self.url_list) == 0:
logger.error(f"无下载清单,任务结束!")
return
for i in self.url_list:
file = str(i).split("/")[-1]
c = "ps | grep wget | grep -v grep"
if ef:
c = "ps -ef| grep wget | grep -v grep"
if system(c) == 0:
logger.warning(f"已存在wget任务,本次取消...")
continue
c = f"wget -c {i}"
logger.debug(f"正在下载:{i} ---> {file}")
if system(c) == 0:
logger.info(f"下载成功,正在删除: {file}")
system(f"rm -f {file}")
else:
logger.error(f"下载失败...")
s = randint(arg_mini, arg_max)
logger.debug(f"本次延迟: {s} 秒")
sleep(s)
def start(self):
self.init_read_txt()
self.init_config()
self.download()
if __name__ == "__main__":
arg = ArgumentParser(description='当前脚本版本: 1.0', prog="流量消失工具-Linux")
home = getenv("HOME")
h = f'设置下载存放路径,默认: {home}'
arg.add_argument('-p', '--path', type=str, default=home, help=h, required=False)
h = f'设置URL清单文件(一行一个URL),默认: None'
arg.add_argument('-t', '--txt', type=str, default=None, help=h, required=False)
h = "设置任务间隔时间随机最小数(单位: 秒),默认: 100"
arg.add_argument('-mini', type=int, default=100, help=h, required=False)
h = "设置任务间隔时间随机最大数(单位: 秒),默认: 1000"
arg.add_argument('-max', type=int, default=1000, help=h, required=False)
h = '进程检测是否使用-ef参数(OpenWrt不需要该参数,主流Linux需要)'
arg.add_argument('-ef', action='store_true', help=h)
args = arg.parse_args()
p = args.path
ef = args.ef
t = args.txt
arg_mini = args.mini
arg_max = args.max
logger.debug(ef)
if path.isdir(p):
chdir(p)
d = Download(txt=t)
d.start()
else:
logger.error(f"路径不存在: {p}")