rakibulinux commited on
Commit
eaa52cd
·
1 Parent(s): 0c9651f

Enhance diagnose function to include TCP connection checks for ports 80 and 443

Browse files
Files changed (1) hide show
  1. api.py +15 -3
api.py CHANGED
@@ -61,15 +61,27 @@ def _check_document(image, endpoint_key: str) -> dict:
61
  def diagnose() -> dict:
62
  # ponytail: one function shows where HF->server path breaks
63
  import socket
 
64
  from urllib.parse import urlparse
65
  out = {"base_url": BASE_URL, "endpoints": ENDPOINTS}
 
 
 
 
66
  try:
67
  p = urlparse(BASE_URL)
68
  host, port = p.hostname, p.port or (443 if p.scheme == "https" else 80)
69
  out["dns"] = socket.gethostbyname(host)
70
- s = socket.create_connection((host, port), timeout=10)
71
- out["tcp_443"] = "ok"
72
- s.close()
 
 
 
 
 
 
 
73
  except Exception as e:
74
  out["tcp_443"] = f"FAIL: {type(e).__name__}: {e}"
75
  return out
 
61
  def diagnose() -> dict:
62
  # ponytail: one function shows where HF->server path breaks
63
  import socket
64
+ import time
65
  from urllib.parse import urlparse
66
  out = {"base_url": BASE_URL, "endpoints": ENDPOINTS}
67
+ try:
68
+ out["egress_ip"] = requests.get("https://api.ipify.org", timeout=10).text
69
+ except Exception as e:
70
+ out["egress_ip"] = f"FAIL: {type(e).__name__}: {e}"
71
  try:
72
  p = urlparse(BASE_URL)
73
  host, port = p.hostname, p.port or (443 if p.scheme == "https" else 80)
74
  out["dns"] = socket.gethostbyname(host)
75
+ for probe_port, key in [(80, "tcp_80"), (443, "tcp_443")]:
76
+ try:
77
+ t0 = time.time()
78
+ s = socket.create_connection((host, probe_port), timeout=10)
79
+ out[key] = f"ok {time.time()-t0:.2f}s"
80
+ s.close()
81
+ except Exception as e:
82
+ out[key] = f"FAIL: {type(e).__name__}: {e}"
83
+ if out.get("tcp_443", "").startswith("FAIL"):
84
+ return out
85
  except Exception as e:
86
  out["tcp_443"] = f"FAIL: {type(e).__name__}: {e}"
87
  return out