Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Requests HTTP range not working

According to this answer I can use the Range header to download only a part of an html page, but with this code:

import requests

url = "http://stackoverflow.com"
headers = {"Range": "bytes=0-100"}  # first 100 bytes

r = requests.get(url, headers=headers)

print r.text

I get the whole html page. Why isn't it working?

like image 696
Hyperion Avatar asked Nov 19 '16 10:11

Hyperion


People also ask

What is range request in HTTP?

An HTTP range request asks the server to send only a portion of an HTTP message back to a client. Range requests are useful for clients like media players that support random access, data tools that know they need only part of a large file, and download managers that let the user pause and resume the download.

What is content range in HTTP header?

The Content-Range response HTTP header indicates where in a full body message a partial message belongs.

What is Accept Ranges?

The Accept-Ranges HTTP response header is a marker used by the server to advertise its support for partial requests from the client for file downloads. The value of this field indicates the unit that can be used to define a range.

What is byte range requests?

Byte-range requests occur when a client asks the server for only a portion of the requested file. The purpose of this is essentially to conserve bandwidth usage by avoiding the need to download a complete file when all that is required is a small section.


1 Answers

If the webserver does not support Range header, it will be ignored.

Try with other server that support the header, for example tools.ietf.org:

import requests

url = "http://tools.ietf.org/rfc/rfc2822.txt"
headers = {"Range": "bytes=0-100"}
r = requests.get(url, headers=headers)
assert len(r.text) <= 101  # not exactly 101, because r.text does not include header
like image 113
falsetru Avatar answered Sep 17 '22 12:09

falsetru