2.发起一个session请求
node2:/root/python/20200525#cat t300.py
import asyncio,aiohttp
# -*- coding: utf-8 -*-
async def fetch_async(url):
print(url)
async with aiohttp.ClientSession() as session:#协程嵌套,只需要处理最外层协程即可fetch_async
async with session.get(url) as resp:
print(resp.url)
print(resp.status)
print(await resp.text())#因为这里使用到了await关键字,实现异步,所有他上面的函数体需要声明为异步async
tasks = [fetch_async('http://192.168.137.3:9000/test111/'), fetch_async('http://192.168.137.3:9000/test222/'),fetch_async('http://192.168.137.3:9000/test333/')]
event_loop = asyncio.get_event_loop()
results = event_loop.run_until_complete(asyncio.gather(*tasks))
event_loop.close()
node2:/root/python/20200525#time
2.发起一个session请求
最新推荐文章于 2024-03-02 11:41:13 发布
本文详细探讨了如何在Web开发中发起一个HTTP Session请求,包括设置Session、管理用户状态和维护会话持久性等方面,旨在帮助开发者更好地理解和实现在应用程序中处理用户交互。
摘要由CSDN通过智能技术生成