From 3109f72f2ac42dc4eea4130ecce213204daf97a9 Mon Sep 17 00:00:00 2001 From: ZeroWolf233 Date: Sun, 1 Dec 2024 22:38:30 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E7=BB=9F=E4=B8=80=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=90=8D=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 40f151a..e2de740 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ import gc # 定义请求函数 -def send_request(url, headers, rest, i): +def sendRequest(url, headers, rest, i): try: timeStart = time.time() response = requests.get(url, headers=headers, timeout=10) # 增加超时设置,防止请求长时间挂起 @@ -24,53 +24,53 @@ def send_request(url, headers, rest, i): fileSize = int(response.headers.get('Content-Length')) / 1024 / 1024 speed = fileSize / timeDiff - print(f"用时 {round(timeDiff, 2)} 秒, 文件大小: {round(fileSize, 2)} MB, 速度 {round(speed, 2)}MB/s\n\n"+"-"*100+"\n") + print(f"用时 {round(timeDiff, 2)} 秒, 文件大小: {round(fileSize, 2)} MB, 速度 {round(speed, 2)}MB/s\n\n"+"-"*100+"\n\n") del response gc.collect() else: - print(f"请求 {i + 1} 失败, 状态码: {response.status_code}\n\n"+"-"*100+"\n") + print(f"请求 {i + 1} 失败, 状态码: {response.status_code}\n\n"+"-"*100+"\n\n") del response gc.collect() except requests.exceptions.RequestException as e: - print(f"请求 {i + 1} 失败, 发生异常: {e}\n\n"+"-"*100+"\n") + print(f"请求 {i + 1} 失败, 发生异常: {e}\n\n"+"-"*100+"\n\n") del response gc.collect() # 每次请求后休息指定时间 time.sleep(int(rest)) -def get_valid_int_input(prompt, default): +def getValidIntInput(prompt, default): while True: - user_input = input(prompt) - if not user_input: + userInput = input(prompt) + if not userInput: return default try: - return int(user_input) + return int(userInput) except ValueError: print("杂鱼杂鱼, 是无效的输入呢~\n") def main(): - url = input('请求地址:') - num_requests = get_valid_int_input('请求次数(默认10次):', 10) + url = input('请求地址: ') + numRequests = getValidIntInput('请求次数(默认10次): ', 10) ua = input('UA(不输入我就用默认的):') or "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0" - rest = get_valid_int_input('请求间隔(单位:秒)(仅支持整数)(默认3秒):', 1) - workers = get_valid_int_input('要多少线程(嘿嘿)(默认1个线程):', 1) + rest = getValidIntInput('请求间隔(单位:秒)(仅支持整数)(默认3秒): ', 1) + workers = getValidIntInput('要多少线程(嘿嘿)(默认1个线程): ', 1) print('\n') headers = {"User-Agent": ua} # 使用 ThreadPoolExecutor 进行多线程请求 with ThreadPoolExecutor(max_workers=workers) as executor: - futures = [executor.submit(send_request, url, headers, rest, i) for i in range(num_requests)] + futures = [executor.submit(sendRequest, url, headers, rest, i) for i in range(numRequests)] # 等待所有线程完成 for future in futures: future.result() + input('运行完成,请查看上方的结果') if __name__ == "__main__": main() - input('运行完成,请查看上方的结果') \ No newline at end of file