I fixed it with a while try loop, waiting for the response to set the variable in order to exit the loop.
When the connection has an exception, it waits five seconds, and continues looking for the response from the connection.
My code before fix, with the failed response HTTPSConnectionPool(host='etc.com', port=443): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001E9955A2050>, 'Connection to example.net timed out. (connect timeout=None)'))
from __future__ import print_function
import sys
import requests
def condition_questions(**kwargs):
proxies = {'https': 'example.com', 'http': 'example.com:3128'}
print(kwargs, file=sys.stdout)
headers = {'etc':'etc',}
body = f'''<etc>
</etc>'''
try:
response_xml = requests.post('https://example.com', data=body, headers=headers, proxies=proxies)
except Exception as ex:
print("exception", ex, file=sys.stdout)
log.exception(ex)
finally:
print("response_xml", response_xml, file=sys.stdout)
return response_xml
After fix, with successful response response_xml <Response [200]>
:
import time
...
response_xml = ''
while response_xml == '':
try:
response_xml = requests.post('https://example.com', data=body, headers=headers, proxies=proxies)
break
except Exception as ex:
print("exception", ex, file=sys.stdout)
log.exception(ex)
time.sleep(5)
continue
finally:
print("response_xml", response_xml, file=sys.stdout)
return response_xml
based on Jatin's answer here --"Just do this,
import time
page = ''
while page == '':
try:
page = requests.get(url)
break
except:
print("Connection refused by the server..")
print("Let me sleep for 5 seconds")
print("ZZzzzz...")
time.sleep(5)
print("Was a nice sleep, now let me continue...")
continue
You're welcome :)"