Created
April 23, 2019 05:49
-
-
Save DoonDoony/a9f227dec8f63d0f8771041e7e3fc2d4 to your computer and use it in GitHub Desktop.
Test handling ReadTimeout Exception
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import logging | |
from requests.exceptions import ReadTimeout, ConnectionError | |
logger = logging.getLogger(__name__) | |
url = 'https://www.google.com:81' | |
data = { 'username': 'DoonDoony', 'address': '...' } | |
retries = 3 | |
try: | |
response = requests.post(url, data=data) | |
except (ReadTimeout, ConnectionError) as error: | |
logger.error('An error occurred while requests') | |
logger.error(error) | |
logger.info(f'URL: {url}, Payload: {data}') | |
logger.info(f'Time elasped: {response.elapsed}') | |
finally: | |
while retries: | |
response = requests.post(url, data=data) | |
logger.info(f'Retrying... {retires}/3') | |
if response.ok: | |
break | |
retries = retries - 1 | |
print(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment