Issue
I am changing the requests version to aiohttp version.
requests
with requests.get(url='url', headers=headers, stream=True) as r:
with open('request_test.mp4', 'wb') as f:
print(int(r.headers['Content-Length']) // (1024 * 10)) # output: 9061.
for index, chunk in enumerate(r.iter_content(chunk_size=1024 * 10)):
print(index) # output: 1 2 3 .... 9061.
f.write(chunk)
aiohttp
async with aiohttp.ClientSession() as session:
async with session.get(url='url', headers=headers, timeout=_timeout) as res:
print(res.content_length // (1024 * 10)) # output: 9061.
with open('aiotest.mp4', 'wb') as fd:
count = 0
while True:
chunk = await res.content.read(1024 * 10)
print(count) # output: 1 2 3 .... 11183
count += 1
if not chunk:
break
fd.write(chunk)
What am I doing wrong?
Please help me.
Solution
res.content.read(1024 * 10)
reads up to 10 KiB per call, not exactly 10 KiB.
The sum of all chunk lengths should be exactly the same as returned by res.content_length
.
Answered By - Andrew Svetlov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.