3

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

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
young_minds1
  • 1,181
  • 3
  • 10
  • 25
  • 2
    If that other question doesn't help you, perhaps there is little you can do. The error message says that it is the remote end (presumably outside of your control) which is closing the connection. Without a [mcve] it is impossible to know what is happening. – John Coleman Mar 24 '21 at 10:44
  • @JohnColeman in other question, the answers says to add send_response(code, message=None) but. where should i add it. that is what i am not understanding – young_minds1 Mar 24 '21 at 10:54
  • 2
    That other question involves both client-side and server-side code, with the problem seeming to be with the server-side code. Your code seems to be just client-side. – John Coleman Mar 24 '21 at 11:43
  • @JohnColeman anything you can suggest? that will help me out? – young_minds1 Mar 24 '21 at 12:17

0 Answers0