File downloads can get interrupted due to network issues, system crashes, or accidental terminations. Instead of restarting from scratch, you can resume the download from where it left off. This blog post shows you how to do that using two powerful tools: curl and Python.
1. Resuming Downloads with curl
curl
makes it simple to resume an interrupted download using the -C -
option.
curl -C - -O https://example.com/largefile.zip
Explanation:
-C -
: Continue/Resume a previous file transfer at the given offset. The dash (-
) tells curl to automatically find the correct byte offset.-O
: Saves the file with its original name.
2. Resuming Downloads with Python
In Python, you can use the requests
module to achieve similar functionality by setting the Range
HTTP header.
Step-by-step Python Script:
import os
import requests
url = 'https://example.com/largefile.zip'
filename = url.split('/')[-1]
# Get existing file size if partially downloaded
resume_header = {}
if os.path.exists(filename):
existing_size = os.path.getsize(filename)
resume_header = {'Range': f'bytes={existing_size}-'}
else:
existing_size = 0
with requests.get(url, headers=resume_header, stream=True) as r:
mode = 'ab' if existing_size else 'wb'
with open(filename, mode) as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
print(f"Download of '{filename}' complete.")
How It Works:
- Checks if the file already exists and determines its size.
- Uses a
Range
header to request only the remaining bytes. - Appends the remaining content to the partially downloaded file.
3. Tips for Reliable Downloads
- Always verify server supports HTTP range requests (check for
Accept-Ranges: bytes
in headers). - Use try-except blocks for robust error handling in production scripts.
Conclusion
Whether you're scripting downloads for automation or recovering from a failed transfer, both curl
and Python provide efficient methods to resume interrupted downloads. Choose the tool that best fits your workflow.
No comments:
Post a Comment