码迷,mamicode.com
首页 > 编程语言 > 详细

python Aiohttp 异步HTTP

时间:2021-05-24 02:57:18      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:app   ons   complete   def   user   url   pytho   pre   apple   

示例

# 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

标签:app   ons   complete   def   user   url   pytho   pre   apple   

原文地址:https://www.cnblogs.com/namejmj/p/14747475.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!