■ HTTPAdapter 클래스의 생성자에서 max_retries 인자를 사용해 HTTP 요청 오류 발생시 재시도하는 방법을 보여준다.
▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
from requests.adapters import HTTPAdapter from requests.sessions import Session def printResponse(title, response): print(title ) print(f" response : {response}" ) print(f" response.url : {response.url}" ) print(f" response.headers['content-type'] : {response.headers['content-type']}") print(f" response.encoding : {response.encoding}" ) print(f" response.status_code : {response.status_code}" ) print() def executeHTTPRequest(method, url, retryCount = 3): with Session() as session: httpAdapter = HTTPAdapter(max_retries = retryCount) session.mount("http://" , httpAdapter) session.mount("https://", httpAdapter) return session.request(method = method, url = url) response = executeHTTPRequest("get", "https://icodebroker.com") printResponse("테스트", response) """ 테스트 response : <Response [200]> response.url : https://icodebroker.com/ response.headers['content-type'] : text/html; charset=UTF-8 response.encoding : UTF-8 response.status_code : 200 """ |
▶ requirements.txt
1 2 3 4 5 6 7 |
certifi==2024.6.2 charset-normalizer==3.3.2 idna==3.7 requests==2.32.3 urllib3==2.2.2 |
※ pip install requests 명령을 실행했다.