import requests import json def test_loki_connection(): url = "http://192.168.50.108:3100/loki/api/v1/query_range" headers = {"Content-Type": "application/json"} # 最小化测试查询 payload = { "query": "{}", "limit": 1, "start": "2023-07-20T00:00:00Z", "end": "2023-07-20T01:00:00Z" } try: r = requests.post(url, data=json.dumps(payload), headers=headers) print(f"状态码: {r.status_code}") print(f"响应: {r.text[:200]}...") # 截断长响应 return r.status_code == 200 except Exception as e: print(f"连接失败: {e}") return False if test_loki_connection(): print("Loki连接测试成功!") else: print("请检查:1. Loki服务状态 2. 网络连接 3. 防火墙设置")