python Aiohttp 异步HTTP

2021-05-30 18:01

阅读:382

标签: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


评论


亲,登录后才可以留言!