Skip to content

Commit

Permalink
chore: 统一变量名格式
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroWolf233 committed Dec 1, 2024
1 parent e6e85dc commit 3109f72
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) # 增加超时设置,防止请求长时间挂起
Expand All @@ -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('运行完成,请查看上方的结果')

0 comments on commit 3109f72

Please sign in to comment.