Aiohttp¶
- 超时
如果不设置 timeout = aiohttp.ClientTimeout(total=30) 超时,不同平台下的超时时间不一样, windows可能是21秒,rk3588可能是130秒。
- 示例
查看...查看...
import aiohttp import asyncio import time async def upload_info(data,url): headers = { "Content-Type": "application/json", "User-Agent": "testforRK3588" } ret = False try: timeout = aiohttp.ClientTimeout(total=30) async with aiohttp.ClientSession(headers=headers, timeout=timeout) as session: async with session.post(url, data = data) as response: result = "" if response.status == 200:#发送成功 result = await response.text()#用于测试 ret = True else: print(f"上传失败,状态码:{response.status}") ret = False except aiohttp.client_exceptions.ClientConnectionError: print("无法连接到服务器,请检查网络连接。") except aiohttp.client_exceptions.ServerDisconnectedError: print("服务器在响应之前断开了连接。") except aiohttp.client_exceptions.ClientResponseError as e: print(f"服务器返回错误状态码: {e.status}") except aiohttp.client_exceptions.ClientPayloadError: print("接收响应体时出现问题。") except Exception as e: print(f"发生未知错误: {str(e)}") return ret if __name__ == '__main__': while True: t_start = time.time() ret = asyncio.run(upload_info("test", "http://10.20.30.26:8080/upload")) print(f"上传耗时:{time.time() - t_start}秒,上传结果:{ret}") time.sleep(1)