42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
"""Explicitly invoked, read-only unauthenticated diagnostics; raw data stays private."""
|
|||
|
|
import json
|
||
|
|
from pathlib import Path
|
||
|
|
import time
|
||
|
|
import urllib.error
|
||
|
|
import urllib.request
|
||
|
|
|
||
|
|
ROOT = Path(__file__).resolve().parents[1]
|
||
|
|
|
||
|
|
|
||
|
|
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
||
|
|
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
||
|
|
return None
|
||
|
|
|
||
|
|
|
||
|
|
def probe(base_url: str) -> list[dict]:
|
||
|
|
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect())
|
||
|
|
results = []
|
||
|
|
for path in ('/', '/info'):
|
||
|
|
start = time.monotonic()
|
||
|
|
result = {'path': path}
|
||
|
|
try:
|
||
|
|
req = urllib.request.Request(base_url.rstrip('/') + path, method='GET')
|
||
|
|
with opener.open(req, timeout=5) as response:
|
||
|
|
body = response.read(65536)
|
||
|
|
result.update(status=response.status,
|
||
|
|
content_type=response.headers.get('content-type'),
|
||
|
|
bytes=len(body))
|
||
|
|
# Deliberately do not log bodies, Location headers, URLs or exception text.
|
||
|
|
except urllib.error.HTTPError as exc:
|
||
|
|
result['status'] = exc.code
|
||
|
|
except (OSError, urllib.error.URLError) as exc:
|
||
|
|
result.update(error_type=type(exc).__name__,
|
||
|
|
reason_type=type(getattr(exc, 'reason', None)).__name__)
|
||
|
|
result['elapsed_seconds'] = round(time.monotonic() - start, 2)
|
||
|
|
results.append(result)
|
||
|
|
return results
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
config = json.loads((ROOT / '.local/environment.json').read_text())
|
||
|
|
print(json.dumps(probe(config['ma_url']), indent=2))
|