python request.post异常
python post报错 退出 Failed to establish a new connection
import requests from requests.exceptions import RequestException # 设置URL和请求数据 url = 'http://example.com/api/resource' data = {'key1': 'value1', 'key2': 'value2'} # 尝试发送POST请求 try: response = requests.post(url, json=data, timeout=10) # 设置超时时间 response.raise_for_status() # 如果请求失败,会抛出HTTPError异常 print(response.json()) # 假设服务器返回JSON响应 except RequestException as e: # 处理所有requests库抛出的异常,包括连接错误 print(f"An error occurred while trying to send the POST request: {e}") # 在这里你可以尝试重试逻辑,例如: # 重试次数和间隔可以根据实际需求调整 retries = 5 retry_interval = 5 # seconds for _ in range(retries): try: response = requests.post(url, json=data, timeout=10) response.raise_for_status() print(response.json()) break # 如果请求成功,则跳出循环 except RequestException as e: print(f"Retrying after {retry_interval} seconds...") time.sleep(retry_interval) else: print("Max retries exceeded. Giving up.")
#########################
QQ 3087438119