Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import requests
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
import time
|
| 7 |
+
from typing import List
|
| 8 |
+
|
| 9 |
+
# 全局进度缓存
|
| 10 |
+
progress_log: List[str] = []
|
| 11 |
+
result_file_path = None
|
| 12 |
+
|
| 13 |
+
# 测试函数,根据你的业务修改这里,这里示例仅做http访问测试
|
| 14 |
+
def test_host(host: str) -> dict:
|
| 15 |
+
try:
|
| 16 |
+
url = host if host.startswith("http") else f"http://{host}"
|
| 17 |
+
resp = requests.get(url, timeout=8)
|
| 18 |
+
return {"host": host, "status_code": resp.status_code, "ok": True, "error": ""}
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return {"host": host, "status_code": 0, "ok": False, "error": str(e)}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def upload_csv(file):
|
| 24 |
+
"""接收上传csv,保存全局引用"""
|
| 25 |
+
global progress_log
|
| 26 |
+
progress_log.clear()
|
| 27 |
+
if file is None:
|
| 28 |
+
return "未选择文件", ""
|
| 29 |
+
progress_log.append(f"✅ 文件已上传: {file.orig_name}")
|
| 30 |
+
return "\n".join(progress_log), file
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def start_batch_test(uploaded_file):
|
| 34 |
+
global progress_log, result_file_path
|
| 35 |
+
progress_log.clear()
|
| 36 |
+
if uploaded_file is None:
|
| 37 |
+
progress_log.append("❌ 请先上传 hosts.csv")
|
| 38 |
+
yield "\n".join(progress_log), None
|
| 39 |
+
return
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
df = pd.read_csv(uploaded_file)
|
| 43 |
+
if "host" not in df.columns:
|
| 44 |
+
progress_log.append("❌ csv必须包含 host 列")
|
| 45 |
+
yield "\n".join(progress_log), None
|
| 46 |
+
return
|
| 47 |
+
host_list = df["host"].dropna().tolist()
|
| 48 |
+
total = len(host_list)
|
| 49 |
+
progress_log.append(f"📋 读取到 {total} 个待测试主机")
|
| 50 |
+
yield "\n".join(progress_log), None
|
| 51 |
+
|
| 52 |
+
out_rows = []
|
| 53 |
+
for idx, h in enumerate(host_list):
|
| 54 |
+
progress_log.append(f"[{idx+1}/{total}] 测试 {h}")
|
| 55 |
+
res = test_host(h)
|
| 56 |
+
out_rows.append(res)
|
| 57 |
+
if not res["ok"]:
|
| 58 |
+
progress_log.append(f" ⚠️失败: {res['error']}")
|
| 59 |
+
else:
|
| 60 |
+
progress_log.append(f" ✅ status:{res['status_code']}")
|
| 61 |
+
yield "\n".join(progress_log), None
|
| 62 |
+
time.sleep(0.2) # 限流,防止CPU/网络打爆
|
| 63 |
+
|
| 64 |
+
# 输出临时csv
|
| 65 |
+
tmp_fd, tmp_path = tempfile.mkstemp(suffix=".csv")
|
| 66 |
+
os.close(tmp_fd)
|
| 67 |
+
pd.DataFrame(out_rows).to_csv(tmp_path, index=False)
|
| 68 |
+
result_file_path = tmp_path
|
| 69 |
+
progress_log.append("\n🎉全部任务完成,可以下载结果文件")
|
| 70 |
+
yield "\n".join(progress_log), tmp_path
|
| 71 |
+
|
| 72 |
+
except Exception as err:
|
| 73 |
+
progress_log.append(f"💥任务异常:{str(err)}")
|
| 74 |
+
yield "\n".join(progress_log), None
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def get_download_file():
|
| 78 |
+
global result_file_path
|
| 79 |
+
return result_file_path
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
with gr.Blocks(title="Csv 🐠") as demo:
|
| 83 |
+
gr.Markdown("# 🐠 批量主机测试工具")
|
| 84 |
+
file_input = gr.File(label="上传 hosts.csv", file_types=[".csv"])
|
| 85 |
+
log_text = gr.Textbox(label="运行日志", lines=15, interactive=False)
|
| 86 |
+
start_btn = gr.Button("🚀 开始测试", variant="primary")
|
| 87 |
+
download_output = gr.File(label="下载测试结果csv")
|
| 88 |
+
|
| 89 |
+
# 上传事件
|
| 90 |
+
file_input.upload(upload_csv, inputs=[file_input], outputs=[log_text, file_input])
|
| 91 |
+
# 启动测试,使用生成器实现实时日志
|
| 92 |
+
start_btn.click(
|
| 93 |
+
fn=start_batch_test,
|
| 94 |
+
inputs=[file_input],
|
| 95 |
+
outputs=[log_text, download_output]
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|