首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >aiohttp的阅读方式与requests.iter_content不同吗?

aiohttp的阅读方式与requests.iter_content不同吗?
EN

Stack Overflow用户
提问于 2021-12-09 15:07:04
回答 1查看 156关注 0票数 0

我将请求版本更改为aiohttp版本。

请求

代码语言:javascript
复制
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) 

艾奥赫特

代码语言:javascript
复制
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)

我做错了什么?

请帮帮我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-17 13:14:42

res.content.read(1024 * 10)每个呼叫最多读取10 KiB,而不是确切地读取10 KiB。

所有块长度的和应该与res.content_length返回的完全相同。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70292177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档