1

i was trying to fill an online form at the time when the traffic of site is very high that it is almost impossible to get response from the server and the form is open for only limited time . So i use python request library to get the response but when i tried this simple code :

import requests
res = requests.get('url')

But i got an 'requests.exceptions.ConnectionError' saying 'An Existing Connection Was Forcibly Closed by the Remote Host'

How can i solve this problem and wait till i get response from request object as much as time it take to get response.

Roman K.C.
  • 49
  • 2
  • 7

1 Answers1

2

according to this question, a solution would be to loop until timeout or result :

import requests
from requests.exceptions import ConnectionError
from time import sleep
i=0
not_found=True
while i <30 and not_found:
    try:
        res = requests.get('url')
        not_found=False
    except ConnectionError:
        sleep(1)
        i += 1
jufx
  • 154
  • 1
  • 7
  • thank you so much for your quick response and that code worked – Roman K.C. Jan 08 '20 at 03:29
  • 1
    is there any other ways we can ask for response such that it doesn't raise Connection Error and it requests until it get response. I mean the script should get stuck at that request statement until it get response from the url. – Roman K.C. Jan 12 '20 at 10:04
  • @RomanK.C. useful content about max_retries on Session() object [here](https://stackoverflow.com/questions/15431044/can-i-set-max-retries-for-requests-request) – jufx Jan 12 '20 at 16:16