Summarize HTTP status classes without retaining request data
Python
Count status classes and exact error codes for a small batch.
Count status classes and exact error codes for a small batch. This stdin-based Python CLI processes one bounded JSON request and returns the documented JSON result. It does not execute jobs or contact external services. Inspect its limits before adapting it to an application.
Before you start
Python 3.12 standard library; no third-party packages. Verified with the recorded pinned runtime.
One UTF-8 JSON object on stdin, no command-line arguments. Maximum encoded request size is 20000 bytes.
Build and check
Save the source as summarize-http-status-codes-python.py.
Save the example JSON below as request.json.
Run python3 summarize-http-status-codes-python.py < request.json.
Compare the parsed JSON result with the expected output. Invalid input exits 2 with empty stdout and a generic stderr message.
Limits to understand
Accepts only an object containing the single key statuses, whose value is an array of at most 1000 integers from 100 through 599; booleans are rejected.
Input is held temporarily in memory for validation and counting; it is not persisted or included in diagnostics.
Counts alone do not establish HTTP retry safety or service health.
Input exactly {"statuses": array of at most 1000 integers 100..599}; booleans forbidden.
Output {"classes":{"1xx":n,"2xx":n,"3xx":n,"4xx":n,"5xx":n},"errors":object}. Errors maps decimal 400..599 status strings to counts; omit zero counts.
Do not infer HTTP retry safety or service health from these counts.
The complete encoded input is limited to 20000 bytes; one additional byte can be read to detect overflow.
This is a bounded local helper. Passing the recorded cases does not verify every input, every Python version or an external integration.
This browser preview builds code locally. It does not contact an API or run the code.
Default example ready.
Python example
import json
import math
import sys
class InvalidInput(ValueError):
pass
def reject_constant(_value):
raise InvalidInput()
def parse_float(value):
number = float(value)
if not math.isfinite(number):
raise InvalidInput()
return number
def unique_object(pairs):
result = {}
for key, value in pairs:
if key in result:
raise InvalidInput()
result[key] = value
return result
def validate_unicode(root):
pending = [root]
while pending:
value = pending.pop()
if isinstance(value, str):
if any(0xD800 <= ord(char) <= 0xDFFF for char in value):
raise InvalidInput()
elif isinstance(value, dict):
pending.extend(value.keys())
pending.extend(value.values())
elif isinstance(value, list):
pending.extend(value)
def main():
try:
if len(sys.argv) != 1:
raise InvalidInput()
raw = sys.stdin.buffer.read(20001)
if len(raw) > 20000:
raise InvalidInput()
text = raw.decode('utf-8', errors='strict')
data = json.loads(
text,
object_pairs_hook=unique_object,
parse_constant=reject_constant,
parse_float=parse_float,
)
validate_unicode(data)
if type(data) is not dict or set(data) != {'statuses'}:
raise InvalidInput()
statuses = data['statuses']
if type(statuses) is not list or len(statuses) > 1000:
raise InvalidInput()
classes = {f'{n}xx': 0 for n in range(1, 6)}
error_counts = {}
for status in statuses:
if type(status) is not int or not 100 <= status <= 599:
raise InvalidInput()
classes[f'{status // 100}xx'] += 1
if status >= 400:
error_counts[status] = error_counts.get(status, 0) + 1
errors = {str(status): error_counts[status]
for status in sorted(error_counts)}
output = json.dumps(
{'classes': classes, 'errors': errors},
separators=(',', ':'),
allow_nan=False,
) + '\n'
except Exception:
sys.stderr.write('Invalid input\n')
return 2
sys.stdout.write(output)
return 0
if __name__ == '__main__':
sys.exit(main())
What to verify after running
Run the documented example and compare the JSON result.
Confirm malformed requests exit 2 without copying input data into diagnostic messages.
20 independent CLI cases passed on this exact source, including an unexpected-argument check.
Request or format preview
Illustrative preview. No provider response, price or balance is fetched.
Code license: MIT. Keep the license notice with your copy.
Example version 1.0.0 · Documentation reviewed 2026-11-15.
20 recorded CLI cases passed in rootless Docker on the pinned Python 3.12 runtime, with 512 MiB RAM, 1 CPU, no network, and read-only source. Other runtimes and all possible inputs are not verified.