Limiting response size with httplib2

Is it possible to limit the size of an httplib2 response? For example, if he sees the HTTP body over X bytes, the connection will simply close without consuming more bandwidth. Or maybe download the first X bytes of the file.

+4
source share
1 answer

Assuming the server sends the response body size to the Content-Length response header field, you can do it yourself.

First call Http.request(method="HEAD") to get only the headers, not the body. Then check the Content-Length field of the response to see if it is below your threshold. If so, make a second request with the correct GET or POST method to retrieve the body; otherwise, an error occurs.

If the server does not give you Content-Length (or lies on it), it doesn't seem like there is a way to disable loading after a certain number of bytes.

+5
source

All Articles