python - aiohttp 帖子的当前上传步骤

标签 python aiohttp

我想知道如何从 aiohttp post 方法获取当前的上传步骤。通常我会使用 get 方法在循环中拉取当前步骤,但如果主机不响应当前上传步骤,这将不起作用。那么有可能得到当前步骤吗?诸如“从 xx% 上传几乎完成”之类的内容。我的意思是等到上传完成很烦人

async def post_task():
    archive =  open("file")
    session = aiohttp.ClientSession()
    post = await session.post("upload_url", data=archive, ssl = False)
    await post.read()
    await session.close()


loop = asyncio.get_event_loop()
loop.run_until_complete(post_task())

最佳答案

您可以尝试使用 streaming uploads结合tqdm.asyncio跟踪文件上传的进度。

或多或少来自流式上传文档:

import asyncio
import os.path

import aiofiles as aiofiles
import aiohttp as aiohttp
from tqdm.asyncio import tqdm


async def file_sender(file_name, chunksize):
    async with aiofiles.open(file_name, "rb") as f:
        chunk = await f.read(chunksize)
        while chunk:
            yield chunk
            chunk = await f.read(chunksize)


def upload_with_progress(file_name=None, chunksize=64 * 1024):
    size = os.path.getsize(file_name)
    return tqdm(file_sender(file_name, chunksize), total=size // chunksize)


# Then you can use file_sender as a data provider:
async def post_task():
    async with aiohttp.ClientSession() as session:
        async with session.post(
            "upload_url",
            data=upload_with_progress("file"),
            ssl=False,
        ) as resp:
            print(await resp.text())


loop = asyncio.get_event_loop()
loop.run_until_complete(post_task())

关于python - aiohttp 帖子的当前上传步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71827532/

相关文章:

python - 使用 aiohttp 无法完全下载图像

Python:用正则表达式从一系列单词中制作一个 python 字典?

python 字节数组

python - Python 上的 Webrtc 无法更改对等点之间的 ICE 连接状态

python-3.x - 如何在使用 aiohttp session.get 发出请求时发送 etag 或上次修改

python - aiohttp:如何从 requests.get 检索 aiohttp 服务器中的数据(正文)

python - 如何将具体的类名作为字符串获取?

python - 使用 BeautifulSoup 获取以 ":"分隔的文本

python - 如何对 aiohttp.web 应用程序进行单元测试

python - 如何解析 aiohttp json future ?