Files
2026-11-01 16:51:06 +08:00

80 lines
2.8 KiB
Python

import base64
from concurrent.futures import ThreadPoolExecutor
import requests
from flask import Flask, jsonify, render_template, request
TIMEOUT = 15 # 单次请求超时秒数
MAX_WORKERS = 20 # 并发线程数
PORT = 5000 # 前端服务端口
BASE_URL = "http://ktl-001.dsfdl-henan.xyz/openApi"
USER = "thzn-datagram0001"
PWD = "thzn-datagram0001"
VENDOR = "mt-datagram"
CITY_LIST = [ # 查询城市,重复的会去重
'福州', '合肥', '北京', '重庆', '杭州', '兰州', '广州', '佛山',
'上海', '南宁', '贵阳', '海口', '石家庄', '郑州', '哈尔滨',
'武汉', '长沙', '长春', '南京', '南昌', '沈阳', '呼和浩特',
'济南', '太原', '西安', '成都', '天津', '拉萨', '乌鲁木齐',
'深圳', '西宁', '银川', '昆明', '苏州', '青岛', '东莞',
'厦门', '无锡', '大连',
]
app = Flask(__name__)
def _auth(username, password):
token = base64.b64encode(f"{username}:{password}".encode()).decode()
return {"Authorization": f"Basic {token}"}
def get_stats(stat_date, vendor_name, username, password, client_id=None):
url = f"{BASE_URL}/getStats"
params = {"date": stat_date, "vendorName": vendor_name}
if client_id:
params["clientId"] = client_id
resp = requests.get(url, headers=_auth(username, password), params=params, timeout=TIMEOUT)
resp.raise_for_status()
return resp.json().get("data", {})
def run_one(stat_date, clientid):
try:
result = get_stats(stat_date, VENDOR, USER, PWD, clientid)
item = {"city": clientid, "success": result["success"], "reject": result["reject"], "error": None}
except Exception as e:
print(clientid, '请求失败', repr(e))
item = {"city": clientid, "success": None, "reject": None, "error": f"{type(e).__name__}: {e}"}
print(clientid, item)
return item
@app.route("/")
def index():
return render_template("index.html")
@app.route("/api/stats")
def api_stats():
stat_date = request.args.get("date", "").strip()
if not stat_date:
return jsonify({"error": "date 不能为空"}), 400
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as pool:
items = list(pool.map(lambda city: run_one(stat_date, city), dict.fromkeys(CITY_LIST)))
total_success = sum(item["success"] for item in items if item["success"] is not None)
total_reject = sum(item["reject"] for item in items if item["reject"] is not None)
failed_cities = [item["city"] for item in items if item["error"]]
return jsonify({
"date": stat_date,
"total_success": total_success,
"total_reject": total_reject,
"failed_cities": failed_cities,
"items": items,
})
if __name__ == "__main__":
print(f"前端地址: http://127.0.0.1:{PORT}")
app.run(host="0.0.0.0", port=PORT, debug=False)