在机顶盒刷个hi-nas,然后进终端,装python3,再开机就运行py文件
systemd
是现代Linux系统中用于初始化系统组件和管理系统进程的标准。以下是创建一个systemd
服务用于开机就启动这个文件,基本步骤:
- 创建一个服务文件,例如
/etc/systemd/system/myservice.service
,内容可能如下:
[Unit]
Description=My Python Service
After=network.target
[Service]
User=yourusername
ExecStart=/usr/bin/python3 /path/to/your_script.py
Restart=always
WorkingDirectory=/path/to/your_script_directory
[Install]
WantedBy=multi-user.target
请将yourusername
替换为你的用户名,/path/to/your_script.py
替换为你的Python脚本的实际路径,/path/to/your_script_directory
替换为你的Python脚本所在的目录。
- 重新加载
systemd
配置以识别新服务:
sudo systemctl daemon-reload
- 启动服务:
复制sudo systemctl start myservice.service
- 使服务在开机时自动启动:
sudo systemctl enable myservice.service
- 检查服务状态:
sudo systemctl status myservice.service
以下是用到的py文件
import requests
import time
import datetime
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://dldir1v6.qq.com/weixin/Windows/WeChatSetup.exe', 'https://qrsj.gdl.netease.com/LauncherGame__697327.exe', 'https://downloadclient03.ztgame.com.cn/ztmianfei/client/zhengtu_setup_20231228.zip', 'https://pm.myapp.com/invc/xfspeed/qqpcmgr/download/QQPCDownload320059.exe', 'https://d4.a.kwimgs.com/kos/nlav12667/5.35.1/KwaiLive-Setup-5.35.1.2005-ReleaseFull.exe', 'https://sta-op.douyucdn.cn/dypc-client/pkg/DYTool/20240510183635704/master_DYToolEX_6.3.1.3.472.exe', 'https://download.huya.com/huyapc/install/HuyaClientInstall.exe']
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, timeout=(10, 30)) # 连接超时10秒,读取超时30秒
if response.status_code == 200:
download_start_time = time.time() # 开始下载的时间
for chunk in response.iter_content(chunk_size=10240): # 按块读取文件内容
if chunk: # 确保chunk不为空
pass # 处理文件内容,例如写入文件
# 检查下载是否超过30分钟
if time.time() - download_start_time > 1000: # 1800秒 = 30分钟
#print(f"Download has exceeded 30 minutes: {url}")
break # 终止下载循环
# except requests.exceptions.RequestException as e: # 捕获requests异常
# print(f"Request failed: {e}")
# except Exception as e: # 捕获其他异常
# print(f"An error occurred: {e}")
finally:
runing -= 1
if response:
response.close() # 关闭下载连接
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:
# 获取当前时间
curtime = datetime.datetime.now()
hour = curtime.hour
if 6 <= hour < 18:
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(95)
elif 18 <= hour < 24:
if (goal == 0 or wasted < goal) and runing < thread:
#logging.info("补充下载链接数:%s", thread - runing)
i = thread - runing - 2
while i > 0:
executor.submit(download, random.choice(urlList))
time.sleep(random.randint(1, 10) / 100)
i -= 1
time.sleep(95)
else:
if (goal==0 or wasted < goal) and runing < thread:
#logging.info("补充下载链接数:%s", thread - runing)
i = thread - runing - 1
while i> 0:
executor.submit(download, random.choice(urlList))
time.sleep(random.randint(1, 10) / 100)
i-=1
time.sleep(95)