[python] Requests 라이브러리를 이용해 응답 헤더를 분석하는 방법은?

다음은 Requests 라이브러리를 사용하여 응답 헤더를 분석하는 예제입니다:

import requests

# GET 요청 보내기
response = requests.get('https://www.example.com')

# 응답 헤더 확인
headers = response.headers

# 헤더 정보 출력
for header, value in headers.items():
    print(f"{header}: {value}")

위 예제에서 response.headersdict 타입의 객체를 반환합니다. headers 객체를 items() 메서드로 순회하여 각 헤더와 값에 접근할 수 있습니다.

예를 들어, Content-Type 헤더 값을 가져오려면 headers['Content-Type'] 또는 headers.get('Content-Type')을 사용할 수 있습니다.

다음은 위 예제를 통해 출력된 일부 헤더 정보입니다:

Server: nginx
Content-Type: text/html; charset=UTF-8
Content-Length: 1234
...

이와 같이 Requests 라이브러리를 사용하여 응답 헤더를 간편하게 분석할 수 있습니다.

참조 문서: