Python requests library giving error "Remote end closed connection without response"

Viewed 3631

I am doing a post call with the help of requests library

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")

even after 5 retries it gives an error

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

and

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

I am using python 3.7 and requests 2.24.0

Looked into this answer but did not help: Python HTTP Server/Client: Remote end closed connection without response error

0 Answers
Related