import urllib.request import json import sys sys.stdout.reconfigure(encoding='utf-8') HA_URL = "http://14.4.4.20:8123/api/" HA_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJkZWNlMWI5ZjJhZDQ0MTRmODU4NGYzN2I1YjgwZDQxMiIsImlhdCI6MTc5MzQ1MjQ2MywiZXhwIjoyMTA4ODEyNDYzfQ.JznpOqsCSnHz6aTVa8K9pQCz1816DC8SrmS1xn1Sm2k" headers = { "Authorization": f"Bearer {HA_TOKEN}", "Content-Type": "application/json" } req = urllib.request.Request(HA_URL, headers=headers) try: with urllib.request.urlopen(req, timeout=5) as r: print("HA API response (http://14.4.4.20:8123/api/):", r.getcode(), r.read().decode()) except Exception as e: print("HA API error (http://14.4.4.20:8123/api/):", e) # Test via https://snarfhome.snarfnet.net/api/ req2 = urllib.request.Request("https://snarfhome.snarfnet.net/api/", headers=headers) import ssl ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE try: with urllib.request.urlopen(req2, timeout=5, context=ctx) as r: print("HA API response (https://snarfhome.snarfnet.net/api/):", r.getcode(), r.read().decode()[:200]) except Exception as e: print("HA API error (https://snarfhome.snarfnet.net/api/):", e)