Max retries exceeded with url 트러블슈팅

소만이·2024년 1월 23일
1
post-thumbnail

request로 HTTP 요청을 보내는 중에 "Max retries exceeded with url" 오류가 났다.
해결 방법을 찾아보니 requests 라이브러리에서는 재시도 횟수를 조절할 수 있다고 한다.

r = requests.get(URL, headers=make_Header())

원래 이렇게 요청을 보내고 있었는데 동일한 웹사이트에 대한 여러 HTTP 요청 간에 상태를 유지하도록 해주는 세션 객체를 생성해 요청을 보내는 걸로 변경하였다.

retry_strategy = Retry(
    total=30,  # Maximum number of retries
    status_forcelist=[429, 500, 502, 503, 504],  # HTTP status codes to retry on
)
session = requests.Session()
# # Create an HTTP adapter with the retry strategy and mount it to session
adapter = HTTPAdapter(max_retries=retry_strategy)
session.mount('http://', adapter)
session.mount('https://', adapter)
session.headers.update({'Connection': 'keep-alive'})


r = session.get(URL, headers=make_Header())

이렇게 바꾸었더니 에러가 더 이상 나지 않았다.

참고 : (https://www.zenrows.com/blog/python-requests-retry#what-to-know)

0개의 댓글