Python请求库给出错误“远程端关闭连接,无响应”

2024-04-25 19:09:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我在图书馆的帮助下打了一个电话

import time

def retry_on_connection_errors(url, data):
    # data consists of dictionary with 20+ key value pairs
    retries = 0
    while retries < 5:
        try:
            response = requests.post(
                url,
                json=data,
                verify=False,
                headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36"}
            )
            return response
        except:
            retries += 1
            time.sleep(1)

    raise Exception("Maximum retries exceeded")

即使重试5次,也会出现错误

requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

我正在使用python 3.7和请求2.24.0

调查了这个答案,但没有帮助: Python HTTP Server/Client: Remote end closed connection without response error


Tags: urldataremotetimeresponseconnectionrequestsexceptions