Bootstrap

flask 异步 app.py示例

flask 异步 app.py

import aiohttp
import asyncio
from flask import Flask

app = Flask(__name__)

async def fetch(session, url):
    print(f"发送请求,地址:{url}")
    async with session.get(url, verify_ssl=False) as response:
        content = await response.content.read()
        file_name = response.headers.get("ETag") + "." + response.headers.get("Content-Type").split("/")[1]
        with open(file_name, "wb") as f:
            f.write(content)
    print(f"下载完成: {url}")


async def main():
    async with aiohttp.ClientSession() as session:
        url_list = [
            "https://img2.baidu.com/it/u=4212406472,810093385&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=625",
            "https://img0.baidu.com/it/u=68353154,235073569&fm=253&fmt=auto&app=120&f
;