python 协程下载文件

异步下载文件

async def download_file_with_curl(url: str, destination: str):
    curl_command = [
        "curl",
        "-k",
        f"{url}",
        "-o",
        destination,
    ]
    process = await asyncio.create_subprocess_exec(
        *curl_command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
    )

    stdout, stderr = await process.communicate()
    logger.debug(stdout.decode().strip())

    if process.returncode != 0:
        logger.debug(stderr.decode().strip())
        # print(stderr.decode().strip())
        raise FileNotDownloadError(f"Curl download failed: {stderr.decode().strip()}")
posted @ 2024-09-12 09:39  vx_guanchaoguo0  阅读(10)  评论(0编辑  收藏  举报