CertGrid CertGrid
Best Practices·Python Automation for IT

Python Retries, Timeouts and Backoff

requests has no default timeout and no retries, so an unattended script gets both wrong by default: it can hang for ever, and it gives up on a failure that would have succeeded a second later. This guide separates connect from read timeouts, builds backoff with jitter, hands the job to urllib3's Retry, and then draws the line - because retrying a 404 is pointless and retrying a POST can create two of something.

APIs and Cloud Guide 28 of 39 Advanced

urllib3.util.retry.Retry uses allowed_methods; the old name method_whitelist was removed in urllib3 2.0. respect_retry_after_header defaults to True.

Everything on this page runs on the control node. Any machine with Python 3 will do.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSControl node - every script in this path runs here2 Core4 GB50 GB

This guide includes

Use this for anything that calls a network service unattended. This matters because requests has no default timeout at all - so one hung server holds your script for as long as it likes.

Before you start

  1. A slow endpoint, with and without a timeout

  2. Connect and read are two different waits

  3. Something worth retrying

  4. The naive retry, and what is wrong with it

  5. Backoff, with jitter

  6. The delays that schedule produces

  7. Or let urllib3 do it

  8. What must not be retried

Official sources