Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions healthcheck/check_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,20 @@
import urllib.error
import urllib.request

TIMEOUT = 2 # seconds


def main(url: str) -> int:
try:
# Ignore HTTP(S)_PROXY, which Docker may inject into containers: the
# target is always the container itself.
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
body = opener.open(url, timeout=TIMEOUT).read().decode()
body = opener.open(url).read().decode()
except urllib.error.HTTPError as exc:
print(f"HTTP {exc.code} from {url}", file=sys.stderr)
return 1
except urllib.error.URLError as exc:
# urlopen() wraps connection-phase failures (refused, DNS, etc.) here.
print(f"{url} unreachable: {exc.reason}", file=sys.stderr)
return 1
except TimeoutError:
# A timeout firing during .read() (after urlopen returns) bubbles up
# as a bare TimeoutError from the underlying socket — not wrapped in
# URLError. Catch it explicitly so the message stays one-line.
print(f"timed out reading {url} after {TIMEOUT}s", file=sys.stderr)
return 1
except OSError as exc:
# ConnectionResetError, etc. — anything else from the socket layer.
print(f"error against {url}: {exc}", file=sys.stderr)
Expand Down
Loading