Size of the original response in bytes

I need to make an HTTP request and determine the size of the response in bytes. I have always used request for simple HTTP requests, but I wonder if I can achieve this using raw?

 >>> r = requests.get('https://github.com/', stream=True) >>> r.raw 

My only problem is that I don’t understand what the raw data is, or how can I read this data type in bytes? Uses request and takes the right approach?

+6
source share
2 answers

Just take len() from the content of the answer:

 >>> response = requests.get('https://github.com/') >>> len(response.content) 51671 

If you want to keep streaming, for example, if the content is too large, you can iterate over pieces of data and summarize 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 
+11
source

r.raw is an instance of urllib3.response.HTTPResponse . We can calculate the length of the response by looking at the response header Content-length or using the built-in len() function.

+1
source

Source: https://habr.com/ru/post/972058/


All Articles