我将请求版本更改为aiohttp版本。
请求
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) 艾奥赫特
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)我做错了什么?
请帮帮我。
发布于 2021-12-17 13:14:42
res.content.read(1024 * 10)每个呼叫最多读取10 KiB,而不是确切地读取10 KiB。
所有块长度的和应该与res.content_length返回的完全相同。
https://stackoverflow.com/questions/70292177
复制相似问题