Python - Requests HTTP content-range not working

1.4k Views Asked by At

can anyone please tell me why i am not able to get the range of contents using content-range header,instead i am getting the whole content in given url.

import  requests
url="https://tools.ietf.org/rfc/rfc2822.txt "
content = requests.get(url)
content_len = int(content.headers['Content-Length'])
print(content_len)
headers = {f"Content-Range": f"bytes=0-100/{content_len}"}
r = requests.get(url, headers=headers)
print(r.content)
1

There are 1 best solutions below

0
Alexander Kvist On

Usually you would use Range and not Content-Range for requesting a subset of a resource. The server will then respond with a HTTP 206 (Partial Content) status and serve you with the requested range. The response will then contain a Content-Range as a header.

The following works for example:

import  requests

url = "https://tools.ietf.org/html/rfc7233"
headers = {"Range": "bytes=0-500"}
r = requests.get(url, headers=headers)