
getallheaders 함수는 HTTP 요청 헤더를 dictionary 형식으로 반환합니다.
이때, 원하는 헤더 이름을 특정으로 가져오려면 dictionary의 키를 사용하면 됩니다. 예를 들어, 'Host' 헤더를 가져오려면 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
python
import requests
response = requests.get('https://www.example.com')
header = response.headers['Host']
print(header)
dictionary에 특정 헤더 이름이 있는지 확인하려면 'in' 연산자를 사용하면 됩니다.
#hostingforum.kr
python
import requests
response = requests.get('https://www.example.com')
if 'Host' in response.headers:
print('Host 헤더가 있습니다.')
else:
print('Host 헤더가 없습니다.')
또한, dictionary의 키를 모두 가져오려면 keys() 메서드를 사용할 수 있습니다.
#hostingforum.kr
python
import requests
response = requests.get('https://www.example.com')
headers = response.headers
for key in headers.keys():
print(key)
2025-03-19 08:34