aiohttp 读取与 requests.iter_content 不同吗?
Is aiohttp read different from requests.iter_content?
我正在将请求版本更改为 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)
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)
我做错了什么?
请帮帮我。
res.content.read(1024 * 10)
读取 每次调用最多 10 KiB,不完全是 10 KiB。
所有块长度的总和应与 res.content_length
返回的完全相同。
我正在将请求版本更改为 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)
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)
我做错了什么?
请帮帮我。
res.content.read(1024 * 10)
读取 每次调用最多 10 KiB,不完全是 10 KiB。
所有块长度的总和应与 res.content_length
返回的完全相同。