• 회원가입
  • 로그인
  • 구글아이디로 로그인

[module] Python - requests.head() 메서드 - 지정 URL에 HEAD 요청 보냄. (= head메서드 = 헤드)

2913  

목차

  1. requests.head() 예제
  2. requests.head() 정의
  3. requests.head() 구문
  4. requests.head() 매개변수

 

requests.head() 예제

 

import requests

x = requests.head('https://homzzang.com/python/demo.php')

print(x.status_code)

print(x.text)

 

 

requests.head() 정의

 

지정 URL에 HEAD 요청 보냄.

 

※ HEAD 요청은 "파일 내용 필요하지 않으나 status_code 또는 HTTP 헤더만 필요"할 때 수행.

 

 

requests.head() 구문

 

requests.head(url, args)

 


[매개변수]

 

url

필수. 요청 url 주소

 

args

선택. 아래 매개변수 중  0개 이상 사용 가능.

 


[반환값]

 

requests.Response 객체 반환.

 

 

requests.head() 매개변수

url 

필수. 요청 URL

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

x = requests.head(url)

 

print(x.headers)

 


allow_redirects 

선택. 리디렉션 허용 여부 선택하는 Boolean.

※ True : 허용. (기본값)

※ False : 비허용. 

 

(예제1)

import requests

url = 'https://homzzang.com/python/demo.php'

x = requests.head(url, allow_redirects=True)

print(x.status_code) # 200 (요청 성공)

 

(예제2)

import requests

url = 'https://homzzang.com/python/demo.php'

x = requests.head(url)

print(x.status_code) # 301 (주소 바뀜)

 


auth

선택. 특정 HTTP 인증을 활성화하는 Tuple.

※ 기본값: None

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

HTTP 기본 인증으로 요청 보냄.

x = requests.head(url, auth = ('user', 'pass')) 

print(x.status_code) # 200 (※ 통신 성공 의미.)

 


cert

선택. 인증서 파일 또는 키를 지정하는 String 또는 Tuple.

※ 기본값: None

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

클라이언트 측 인증서로 사용할 인증서 지정.

x = requests.head(url, cert='cert/hz.cert')

print(x.status_code) # 200 (※ 통신 성공 의미.)

 


cookies

선택. 지정 URL로 보낼 쿠키 Dictionary.

※ 기본값: None

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

# 서버에 쿠키 전송

x = requests.head(url, cookies = {"color": "red"})

print(x.status_code) # 200 (※ 통신 성공 의미.)

 


headers

선택. 지정 URL로 보낼 HTTP 헤더 Dictionary.

※ 기본값: None

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

# HTTP 헤더 설정.

x = requests.head(url, headers = {"HTTP_HOST": "홈짱닷컴"})

print(x.status_code) # 200 (※ 통신 성공 의미.)

 


proxies

선택. 프록시 URL에 대한 프로토콜 Dictionary.

※ 기본값: None

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

# 무료 프록시 주소 찾아 해당 프록시 통해 요청 보냄.

x = requests.head(url, proxies = { "https" : "https://1.1.0.1:80"})

print(x.status_code) # 200 (※ 통신 성공 의미.)

 


stream

선택. 응답 스트리밍 허용 여부 Boolean.

※ False : 허용 X (즉, 다운로드). (기본값)

※ True : 허용 O. (즉, 스트리밍).

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

# 스트리밍 허용.

x = requests.head(url, stream=True)

print(x.status_code) # 200 (※ 통신 성공 의미.)

 


timeout

선택. 클라이언트가 연결 (그리고/또는) 응답을 보낼 때까지 기다리는 시간 (초) 나타내는 Number 또는 Tuple.

※ 기본값: None. (즉, 연결 닫힐 때까지 요청 계속됨 의미.)

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

# timeout 설명 위해, 연결 시간 초과되게 매우 짧게 시간 설정.

x = requests.head(url, timeout=0.001)

print(x.status_code) # 200 (※ 통신 성공 의미.)

 


verify

선택. 서버 TLS 인증서 확인하는 Boolean 또는 문자열 표시.

※ True : 확인. (기본값)

※ False : 비확인.

 

(예제1)

import requests

url = 'https://homzzang.com/python/demo.php'

# TLS 인증서 경로 사용해 요청.

x = requests.head(url, verify='cert/tlscertificate')

print(x.status_code) # 200 (※ 통신 성공 의미.)

 

(예제2)

import requests

url = 'https://homzzang.com/python/demo.php'

# TLS 인증서 경로 사용 않고 요청.

x = requests.head(url, verify=False)

print(x.status_code) # 200 (※ 통신 성공 의미.)

 



분류 제목
module Python - random 모듈 메서드 종류
module Python - random.seed() 메서드 - 난수 생성기 초기화 (= seed메서드 = 시드)
module Python - random.getstate() 메서드 - 난수 생성기 현재 상태 반환. (= getstat…
module Python - random.setstate() 메서드 - 난수 생성기 상태 복원. (= setstate메서…
module Python - random.getrandbits() 메서드 ★ - 지정 bit 크기의 정수 반환. (= g…
module Python - random.randrange() 메서드 ★★ - 지정 범위 안에서 정수형 난수 반환. (=…
module Python - random.randint() 메서드 ★★ - 지정 범위 안 int형 난수 생성. (= ra…
module Python - random.choice() 메서드 ★★ - 요소 랜덤 반환. (= choice메서드 = 초…
module Python - random.choices() 메서드 - 가중치 반영해 랜덤 요소 반환. (= choices…
module Python - random.shuffle() 메서드 ★★ - 요소 순서 뒤섞기. (= 순서 랜덤 = shu…
module Python - random.sample() 메서드 ★ - 일부 요소 랜덤 선택. (= sample메서드 =…
module Python - random.random() 메서드 ★ - 0 ~ 1 사이 부동소수 랜덤 반환. (= ran…
module Python - random.uniform() 메서드 ★ - 지정 범위 안 랜덤 부동소수 반환. (= uni…
module Python - random.triangular() 메서드 - 지정 범위 안 가중치 반영 랜덤 부동소수 반환…
module Python - random.betavariate() 메서드 △ - 베타분포 (통계용) 기반 0~1 사이 랜…
module Python - random.expovariate() 메서드 △ - 지수분포 (통계용) 기반 랜덤 부동소수 …
module Python - random.gammavariate() 메서드 △ - 감마분포 (통계용) 기반 랜덤 부동소수…
module Python - random.gauss() 메서드 △ - 가우스분포 (확률이론용) 기반 랜덤 부동소수 반환.
module Python - random.lognormvariate() 메서드 △ - 로그정규분포 (확률이론용) 기반 랜…
module Python - random.normalvariate() 메서드 △ - 정규분포 (확률이론용) 기반 랜덤 …
1/7
목록
찾아주셔서 감사합니다. Since 2012