Interview Prep/Python for DevOps/Data formats, HTTP & REST APIs

How do you download a large file without loading it into memory?

IntermediateConceptData formats, HTTP & REST APIs
Answer

Stream it in chunks and verify a checksum:

import hashlib
h = hashlib.sha256()
with s.get(url, stream=True, timeout=(5, 60)) as r:
    r.raise_for_status()
    with open(dest, "wb") as f:
        for chunk in r.iter_content(chunk_size=1 << 20):
            f.write(chunk)
            h.update(chunk)
if h.hexdigest() != expected_sha256:
    raise ValueError("checksum mismatch")

Always verify checksums for binaries you install (kubectl, terraform, helm).

All Python for DevOps questions Roadmap: Python for automation

More on Data formats, HTTP & REST APIs