Enhance fetch_targets with error handling and timeout

Added error handling and timeout to fetch_targets function.
This commit is contained in:
Roy
2026-06-06 07:08:45 -07:00
committed by GitHub
parent be8bd97c55
commit cd124132cc
+21 -8
View File
@@ -1839,14 +1839,27 @@ THEMEMUSIC_CODE = r"""(function () {
# Utility: Fetch debugger targets
def fetch_targets(host, port):
conn = http.client.HTTPConnection(host, port)
conn.request("GET", "/json")
resp = conn.getresponse()
if resp.status != 200:
raise Exception(f"Failed to fetch targets: {resp.status} {resp.reason}")
data = resp.read()
conn.close()
return json.loads(data)
try:
conn = http.client.HTTPConnection(host, port, timeout=5)
conn.request("GET", "/json")
resp = conn.getresponse()
if resp.status != 200:
print(f"Failed to fetch targets: {resp.status} {resp.reason}")
sys.exit(0)
data = resp.read()
return json.loads(data)
except (ConnectionRefusedError, socket.timeout, http.client.CannotSendRequest, http.client.RemoteDisconnected, Exception) as e:
print(f"Error fetching debugger targets: {e}")
sys.exit(0)
finally:
try:
conn.close()
except:
pass
# Find websocket debugger URL for the target title
def get_ws_url_by_title(host, port, title):