Created
October 27, 2014 01:39
-
-
Save ARezaK/fb16024682d06d7d8b67 to your computer and use it in GitHub Desktop.
Auto retrying GET using requests
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
def fetch_url(url, num_of_tries): | |
i = 0 | |
while i < num_of_tries: | |
try: | |
content = requests.get(url, verify=False) | |
if content.status_code != 200: | |
print content.status_code | |
print content.text | |
raise Exception("Not 200") | |
return content | |
except Exception as e: | |
print("Exception requesting " + str(url) + " Error: " + str(e)) | |
i += 1 | |
time.sleep(2 * i) | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment