Home > database >  Python requests stream reads more data than chunk size
Python requests stream reads more data than chunk size

Time:01-31

I am using python requests library to stream the data from a streaming API.

response = requests.get('http://server/stream-forever', stream=True)
for chunk in response.iter_content(chunk_size=1024):
    print len(chunk) # prints 1905, 1850, 1909

I have specified the chunk size as 1024. Printing the length of the chunk read gives the chunk size greater than 1024 like 1905, 1850, 1909 and so on.

Am I missing something here?

CodePudding user response:

According to requests.Response.iter_content docs

(...)The chunk size is the number of bytes it should read into memory. This is not necessarily the length of each item returned as decoding can take place.(...)

  • Related