侧边栏壁纸
博主头像
海绵

正向的根基决定逆向的上限

  • 累计撰写 32 篇文章
  • 累计创建 29 个标签
  • 累计收到 21 条评论

aiohttp异步模块使用

海绵
2022-01-12 / 0 评论 / 1 点赞 / 606 阅读 / 522 字
import aiohttp
import asyncio

concurrency = asyncio.Semaphore(500)  # 最大并发量

async def get_baidu(session,url):
    async with session.get(url) as res:
        return res.status,await res.text()


async def main():
    url = 'http://www.51pyblog.com'
    timeout = aiohttp.ClientTimeout(total=10)  # 超时时间
    async with concurrency:
        async with aiohttp.ClientSession(timeout=timeout) as session:
            status,text = await get_baidu(session,url)
            print(status)

if __name__ == '__main__':
    task = [asyncio.ensure_future(main()) for _ in range(2000)]
    loop = asyncio.get_event_loop().run_until_complete(asyncio.wait(task))
0

评论区