Python教程

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())
	```
这篇关于python Aiohttp 异步HTTP的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!