Skip to content

Instantly share code, notes, and snippets.

@DxDiagDx
Created September 29, 2022 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DxDiagDx/d18cda5ff28cf9c114c6047b2f63fcda to your computer and use it in GitHub Desktop.
Save DxDiagDx/d18cda5ff28cf9c114c6047b2f63fcda to your computer and use it in GitHub Desktop.
Python Requests — запрос в несколько попыток
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