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())
	```
上一篇:用aiohttp和uvloop实现一个高性能


下一篇:LOJ3213 「CSP-S 2019」树的重心