编程语言
首页 > 编程语言> > python Aiohttp 异步HTTP

python Aiohttp 异步HTTP

作者:互联网

示例

# pip install aiohttp
import asyncio
import aiohttp

headers = {
    "Referer": "https://vod.bunediy.com",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
                  "(KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36",
}

async def Job(url):
    """处理函数"""
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            print("Status:", response.status)
            print("Content-type:", response.headers['content-type'])
            # print(await response.text())
            # await response.json()
            # byes=await response.content.read()


async def main():
    # 任务入队
    urls = ["http://ww.cn", "http://ww.cn", "http://ww.cn"]
    task = []
    for i in urls:
        task.append(asyncio.create_task(Job(i)))
    await asyncio.wait(
        task
    )


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
	```

标签:__,task,HTTP,Aiohttp,python,await,async,response,asyncio
来源: https://www.cnblogs.com/namejmj/p/14747475.html