December 5, 2026

HTTP Range Requests: How Downloads Resume, Tested With curl, Python and a Server That Supports Them

A laptop showing a stalled download progress bar beside a router

A download dies at 40 percent, and the next attempt picks up where it stopped instead of starting over. That works because of one request header, Range, and one response code, 206 Partial Content. When either side doesn’t support them, the “resume” silently becomes a full re-download, or worse, a corrupted file. A test server built for this post, a 1,000,000-byte file of random data, and curl and Python as clients show each piece of the exchange, including the one case where a server should refuse to resume and why that refusal protects the file.

Video: WittCode on YouTube

The exchange

Asking for part of a file

A client asks for bytes by number. Range: bytes=0-99 means the first hundred bytes. bytes=400000- means everything from byte 400,000 to the end. bytes=-500 means the last 500 bytes.

What came back

Requesting the first hundred bytes from the test server with curl -r 0-99 returned:

HTTP/1.1 206 Partial Content
Content-Range: bytes 0-99/1000000
Accept-Ranges: bytes
ETag: "ed0179a4bfe6e6f8"
Content-Length: 100

The Content-Range header names the slice and the total size. RFC 9110, the standard for HTTP semantics, says a server generating a 206 response “MUST generate a Content-Range header field” for a single-part response. MDN’s range requests guide adds the check a client can make first: if a response includes Accept-Ranges with any value other than none, the server supports range requests.

The suffix form

Range: bytes=-500 returned Content-Range: bytes 999500-999999/1000000, the last 500 bytes. That form is how media players and file tools read the end of a file, where some formats keep their index, without downloading the rest.

Yellow duct tape on blue surface

Resuming a download

With curl

The test copied the first 400,000 bytes of the file into part.bin, as if an earlier download had been cut off, then ran:

curl -C - -o part.bin http://127.0.0.1:8769/f
cmp part.bin big.bin && echo resumed-ok

The -C - option tells curl to look at the existing file’s size and request the rest with a Range header. The result was 1,000,000 bytes, byte-for-byte identical to the original, and cmp printed resumed-ok.

With Python

The same idea in Python, using requests:

import os, requests
def resume(url, path, etag=None):
    have = os.path.getsize(path) if os.path.exists(path) else 0
    headers = {"Range": f"bytes={have}-"}
    if etag:
        headers["If-Range"] = etag
    r = requests.get(url, headers=headers, stream=True, timeout=(3.05, 30))
    mode = "ab" if r.status_code == 206 else "wb"
    with open(path, mode) as f:
        for chunk in r.iter_content(65536):
            f.write(chunk)
    return r.status_code

The key line is the mode choice. A 206 means the server sent only the remaining bytes, so they’re appended. A 200 means the server sent the whole file, so the old partial copy must be overwritten, not appended to.

Three runs of that function

Starting file Server and If-Range Status Result
first 300,000 real bytes range-capable, correct ETag 206 1,000,000 bytes, hash matches
300,000 bytes of junk range-capable, wrong ETag 200 overwritten, 1,000,000 bytes, hash matches
first 300,000 real bytes Python http.server, no If-Range 200 overwritten, 1,000,000 bytes

Every run ended with a complete, correct file, including the one that started from garbage, because the status code decided between appending and starting over.

When the server says no

Ranges past the end

Asking for bytes=2000000- on a one-million-byte file returned HTTP/1.1 416 Range Not Satisfiable. RFC 9110 says a server should send a 416 when a range is unsatisfiable, and MDN describes it as a request where none of the range values overlap the resource. A resuming client that gets 416 has usually already finished the download.

Servers that ignore Range

RFC 9110 also says plainly that a server may ignore the Range header field. MDN describes the result: when range requests aren’t supported, a 200 OK comes back with the entire body. Python’s built-in http.server module is one example. Asked for bytes 0 to 99 of the same file, it answered HTTP/1.0 200 OK with Content-Length: 1000000 and no Accept-Ranges header. A client that blindly appended that response to a partial file would end up with a corrupted file larger than the original. That’s why the Python function above checks the status code before choosing between append and overwrite.

A piece of white paper that has been torn

If-Range: don’t stitch two different files together

The danger

Suppose a file changes on the server between the interrupted download and the resume, say a new version of an installer is uploaded under the same name. Appending the second half of the new file to the first half of the old one produces something that matches neither. Nothing about the byte counts would reveal it.

The safeguard

RFC 9110 describes If-Range informally as a request that says: if the representation is unchanged, send the parts requested in Range; otherwise send the entire representation. The client sends the ETag, a version identifier, that it saw with the first part. The test server’s ETag was "ed0179a4bfe6e6f8". Sending Range: bytes=0-99 with If-Range: "stale" returned HTTP/1.1 200 OK and the full file, exactly as intended. The client then knows to start over.

The full decision

Response Meaning What to do
206 Partial Content server sent the requested range, file unchanged append
200 OK no range support, or the file changed overwrite from scratch
416 Range Not Satisfiable the requested start is past the end usually already complete; verify size

Parallel downloads

Splitting a file into ranges

Download managers use ranges to fetch several parts of one file at once. A four-thread test asked the server for the file’s size with a one-byte range, then requested four quarters at the same time and joined them in order:

import requests, hashlib, concurrent.futures as cf
url = "http://127.0.0.1:8769/f"
n = int(requests.get(url, headers={"Range": "bytes=0-0"})
        .headers["Content-Range"].split("/")[1])
sz = n // 4
def get(i):
    a = i * sz
    b = n - 1 if i == 3 else (i + 1) * sz - 1
    r = requests.get(url, headers={"Range": f"bytes={a}-{b}"})
    return i, r.content
with cf.ThreadPoolExecutor(4) as ex:
    parts = dict(ex.map(get, range(4)))
data = b"".join(parts[i] for i in range(4))
print(len(data), hashlib.sha256(data).hexdigest()[:16])
# 1000000 ed0179a4bfe6e6f8

The reassembled file matched the original’s hash. On a fast local connection there’s nothing to gain, but over a link where each connection is throttled separately, parallel ranges can multiply throughput. Many servers limit how many connections one client may open, and some treat aggressive parallel downloading as abuse, so a polite downloader keeps the count low.

A close up of a network switch box

What a real CDN did

Testing this site’s own files

The same requests were sent from a browser to two files served by x9m8.com’s content delivery network, which identified itself as hcdn. The first was an 86,636-byte WebP image:

Request Result
Range: bytes=0-99 206, Content-Range: bytes 0-99/86636, 100 bytes
Range: bytes=0-9,100-109 206, multipart/byteranges, a 222-character body holding both slices
Range: bytes=99999999- 416, Content-Range: bytes */86636

That’s textbook behavior, including the multipart response MDN describes for several ranges at once, with each slice wrapped in its own part and separated by a boundary string.

The compressed file

The second file was a JavaScript library, sent with Brotli compression (Content-Encoding: br). Its response advertised Accept-Ranges: bytes, yet the same Range: bytes=0-99 request came back as a 200 with the entire 87,553 bytes, and so did the multi-range and out-of-range requests. The server promised range support and then, for a file it compresses on the fly, quietly ignored ranges, which RFC 9110 allows. A client that trusted the Accept-Ranges header alone and appended the response to a partial file would have corrupted it. The status code, not the advertisement, is the thing to trust.

Home lab notes

What to check on a self-hosted file server

  • Request a small range with curl -r 0-99 -D - -o /dev/null and look for 206 and Content-Range.
  • Make sure the server sends an ETag or Last-Modified header so clients can use If-Range.
  • Avoid Python’s http.server for large files people will download over flaky links. As tested here, it ignores ranges, so every interrupted download starts over.
  • Reverse proxies and compression can interfere: a response compressed on the fly has no fixed byte positions, so range requests against it may fail or fall back to a full download.

Verifying the result

Whatever the method, a hash check at the end is the only proof the file is intact. Many download pages publish a SHA-256 checksum for exactly this reason. Comparing it after a resumed download takes a second and catches the stitched-file problem even when If-Range wasn’t used. On Linux, sha256sum file.iso prints the hash; on macOS the same job is done by shasum -a 256 file.iso. Matching only the first and last few characters by eye is a common shortcut, but pasting both hashes into a comparison, or letting sha256sum -c check a published checksum file, removes any doubt.

Sources

  • IETF, RFC 9110, “HTTP Semantics” (June 2022): https://www.rfc-editor.org/rfc/rfc9110.html
  • MDN Web Docs, “HTTP range requests”: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Range_requests

920 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *