Size of raw response in bytes
Question:
I need to make a HTTP request and determine the response size in bytes. I have always used request
for simple HTTP requests, but I am wondering if I can achieve this using raw?
>>> r = requests.get('https://github.com/', stream=True)
>>> r.raw
My only problem is I don’t understand what raw returns or how I could count this data-type in bytes? Is using request
and raw the right approach?
Answers:
Just take the len()
of the content of the response:
>>> response = requests.get('https://github.com/')
>>> len(response.content)
51671
If you want to keep the streaming, for instance if the content is (too) large you can iterate over chunks of the data and sum their sizes:
>>> with requests.get('https://github.com/', stream=True) as response:
... size = sum(len(chunk) for chunk in response.iter_content(8196))
>>> size
51671
r.raw
is an instance of urllib3.response.HTTPResponse. We can count the length of response by looking up the response’s header Content-length
or use built-in function len()
.
I need to make a HTTP request and determine the response size in bytes. I have always used request
for simple HTTP requests, but I am wondering if I can achieve this using raw?
>>> r = requests.get('https://github.com/', stream=True)
>>> r.raw
My only problem is I don’t understand what raw returns or how I could count this data-type in bytes? Is using request
and raw the right approach?
Just take the len()
of the content of the response:
>>> response = requests.get('https://github.com/')
>>> len(response.content)
51671
If you want to keep the streaming, for instance if the content is (too) large you can iterate over chunks of the data and sum their sizes:
>>> with requests.get('https://github.com/', stream=True) as response:
... size = sum(len(chunk) for chunk in response.iter_content(8196))
>>> size
51671
r.raw
is an instance of urllib3.response.HTTPResponse. We can count the length of response by looking up the response’s header Content-length
or use built-in function len()
.