"""Reproduce the retry-after example with Python 3.9+. All inputs are synthetic. Runs offline.""" import argparse from datetime import datetime, timezone from email.message import Message from email.utils import parsedate_to_datetime import json import re def result(key, method, columns, rows, limits): return dict(key=key, method=method, columns=columns, rows=rows, limits=limits) def delay(value, now): """Only decimal seconds and the single HTTP date format used by these fixtures.""" if re.fullmatch('[0-9]+', value): return int(value) if not re.fullmatch(r'[A-Z][a-z]{2}, \d\d [A-Z][a-z]{2} \d{4} \d\d:\d\d:\d\d GMT', value): return None target = parsedate_to_datetime(value) return max(0, int((target-now).total_seconds())) def retry_after(): rows = [] for minute in (0, 3): now = datetime(2026,1,1,12,minute,tzinfo=timezone.utc) for raw in ('120', 'Thu, 01 Jan 2026 12:02:00 GMT', '-5'): headers = Message() headers['Retry-After'] = raw seconds = delay(headers['Retry-After'], now) rows.append([now.strftime('%H:%M UTC'), raw, '∅' if seconds is None else f'{seconds} s']) return result('retry-after', 'Interpret a synthetic 503 response header at receipt on 1 January 2026. Two otherwise ' 'identical clients read 12:00 UTC and 12:03 UTC on their clocks at the same instant. ' 'Decimal seconds are counted from receipt. For a fixed HTTP date subtract the client ' 'clock and clamp a past time to zero. ∅ means the value is rejected by this parser.', ['Client clock', 'Retry-After', 'Calculated wait'], rows, 'This is arithmetic on synthetic headers. The parser covers the integer form and the ' 'modern HTTP date format used here. It is not a complete HTTP implementation. Zero ' 'means the date has passed according to that clock. No retry is sent. A crawler may ' 'ignore the header or choose another time. This does not measure recrawl frequency.') if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('case', nargs='?', choices=['retry-after']) parser.parse_args() print(json.dumps(retry_after(), indent=2, ensure_ascii=False))