Created
September 29, 2022 12:26
-
-
Save DxDiagDx/d18cda5ff28cf9c114c6047b2f63fcda to your computer and use it in GitHub Desktop.
Python Requests — запрос в несколько попыток
This file contains 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 time | |
import requests | |
def get_response(metod, attempts=3, pause=3, **kwargs): | |
response = None | |
for attempt in range(1, attempts + 1): | |
try: | |
if 'get' in metod: | |
response = requests.get(**kwargs) | |
if 'post' in metod: | |
response = requests.post(**kwargs) | |
except Exception as err: | |
print(err) | |
time.sleep(pause) | |
continue | |
if response.status_code == 404: | |
print('Error 404') | |
break | |
if response.status_code == 200: | |
break | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment