4.1 ์๋ต ์ฒ๋ฆฌ
requests
๋ชจ๋์ ์๋ฒ ์๋ต์ ๋ค๋ฃจ๊ธฐ ์ํ ํธ๋ฆฌํ ๋ฉ์๋๋ฅผ ์ ๊ณตํด.
์ํ ์ฝ๋
์๋ต๊ณผ ํจ๊ป ์๋ฒ๊ฐ ์์ฒญ ์ฒ๋ฆฌ ์ํ๋ ๋ณด๋ด์ง. ์ํ์ ๋ํ ์ ๋ณด๋
status_code
์ reason
ํ๋์ ์์ด. ์๋ ์์ ๋ฅผ ๋ด๋ด:
import requests
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print(response.status_code) # ์๋ต์ ์ํ ์ฝ๋๋ฅผ ์ถ๋ ฅ
print(response.reason) # ์ํ์ ๋ํ ์ค๋ช
์ ์ถ๋ ฅ
print(response.ok) # ์ํ ์ฝ๋๊ฐ 400 ๋ฏธ๋ง์ด๋ฉด True ๋ฐํ
ํค๋
๋น์ฐํ HTTP ์์ฒญ์ ํค๋ ์์ด ๋ชป ๊ฐ์ง! ์์ฒญ ํค๋๋ ์๋ต ํค๋๊ฐ ํ์ํ๋ฉด
headers
ํ๋๋ฅผ ํตํด ์ ๊ทผํ ์ ์์ด:
import requests
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print(response.headers) # ์๋ต ํค๋๋ฅผ ์ถ๋ ฅ
print(response.headers['Content-Type']) # ํน์ ํค๋ ๊ฐ์ ์ถ๋ ฅ
์๋ต ๋ณธ๋ฌธ
์๋ฒ ์๋ต์๋ ๋ฐ์ดํธ, ํ
์คํธ, json
๋๋ xml
์ด ํฌํจ๋ ์ ์์ด.
์๋ฒ์ ์์ฒญํ ๋ด์ฉ์ ์๊ณ ์๋ค๋ฉด,
์ํ๋ ํ์
์ ๊ฐ์ฒด๋ฅผ ๋ฐ๋ก ์ป๊ธฐ ์ํด
๋ฉ์๋๋ ํ๋๋ฅผ ์ฌ์ฉํ ์ ์์ด:
import requests
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print(response.text) # ํ
์คํธ ํํ๋ก ์๋ต ๋ณธ๋ฌธ ์ถ๋ ฅ
print(response.json()) # JSON ํํ๋ก ์๋ต ๋ณธ๋ฌธ ์ถ๋ ฅ
print(response.content) # ๋ฐ์ดํธ ํํ๋ก ์๋ต ๋ณธ๋ฌธ ์ถ๋ ฅ
ํค๋ (headers)
์ ์๋ต ์ํ์ ๋ํ ์์ธํ ๋ด์ฉ์ ๋คํธ์ํฌ์ ์๋ ๋ฐฉ์์ ๋ํ ๊ฐ์์์ ๋ค๋ฃฐ ๊ฑฐ์ผ.
4.2 ์๋ฌ ์ฒ๋ฆฌ
requests
๋ชจ๋์ ์๋ฌ๋ฅผ ๋ค๋ฃฐ ์ ์๋ ์์ธ๋ฅผ ์ ๊ณตํด.
HTTP ํ์ค์ ์์ธ๋ฅผ ์ ๊ณตํ์ง ์์, ๋์ ์ ์๋ฌ ์ฝ๋๋ฅผ (status_code)
์ฌ์ฉํด.
์์ฒญ ์คํจ ์ Python ์์ธ๋ฅผ ๋ฐ์์ํค๊ณ ์ถ๋ค๋ฉด,
ํจ์ raise_for_status()
๋ฅผ ๋ช
์์ ์ผ๋ก ํธ์ถํด์ผ ํด.
์์ :
import requests
try:
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
response.raise_for_status() # 4xx ๋ฐ 5xx ์ํ ์ฝ๋๋ฅผ ์ํ ์์ธ ๋ฐ์
except requests.exceptions.HTTPError as err:
print(f"HTTP error occurred: {err}")
except Exception as err:
print(f"Other error occurred: {err}")
else:
print("Success!")
์๋ฌ ์ฝ๋์ ๋ํ ์์ธํ ๋ด์ฉ์ ๋คํธ์ํฌ์ ์๋ ๋ฐฉ์์ ๋ํ ๊ฐ์์์ ์์๋ณผ ์ ์์ด.
4.3 ๋ฐ์ดํฐ ์ ์ก
JSON์ด ๋ฐ๋ช
๋๊ธฐ ์ ์๋ ๋๋์ ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๊ธฐ ์ํด "ํผ"์ ์ฌ์ฉํ์ด.
ํผ์ ๋ธ๋ผ์ฐ์ ํ์ด์ง์ ํน๋ณํ ๊ฐ์ฒด์ด์ HTTP์์์ ๋ฐ์ดํฐ ํ์ค์ด์ผ.
๋ฐ์ดํฐ๋ฅผ ์ ์กํ๊ณ ์ถ๋ค๋ฉด ์์ฒญ๊ณผ ํจ๊ป data
ํ๋ผ๋ฏธํฐ๋ฅผ ์ ๋ฌํ๋ฉด ๋ผ.
์ค์!
GET ์์ฒญ์ ์์ฒญ ๋ณธ๋ฌธ์ ํฌํจํ์ง ์๊ธฐ ๋๋ฌธ์
๋ฐ์ดํฐ๋ฅผ ์ ์กํ ์ ์์ด. ๋ชจ๋ ๋ฐ์ดํฐ๋ URL์๋ง ํฌํจ๋ผ.
GET ์์ฒญ
์ผ๋ก ๋ฐ์ดํฐ ์ ์ก
GET ์์ฒญ
์์๋ URL์ ํ๋ผ๋ฏธํฐ๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ ์กํด. ์์ ๋ฅผ ๋ด๋ด:
import requests
params = {'key1': 'value1', 'key2': 'value2'}
response = requests.get('https://httpbin.org/get', params=params)
print(response.url) # ์ถ๊ฐ๋ ํ๋ผ๋ฏธํฐ์ ํจ๊ป URL ์ถ๋ ฅ
ํผ ๋ฐ์ดํฐ ์ ์ก
๋ฐ์ดํฐ๋ฅผ ์๋ฒ๋ก ์ ์กํ๊ธฐ ์ํด POST ์์ฒญ
์ ์ฌ์ฉํ ๊ฑฐ์ผ.
์์ :
import requests
data = {
'username': 'example',
'password': 'password'
}
response = requests.post('https://httpbin.org/post', data=data)
print(response.json())
ํ์ผ ์ ์ก
์ธํฐ๋ท์ผ๋ก ์ด์ง ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๊ณ ์ถ๋ค๋ฉด, ์๋ฅผ ๋ค์ด ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ ๊ฒฝ์ฐ,
files
ํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ์ ์์ฒญ์ ์ฒจ๋ถํ๋ฉด ๋ผ.
์์ :
import requests
files = {'file': open('example.txt', 'rb')}
response = requests.post('https://httpbin.org/post', files=files)
print(response.json())
์ ๋ง ๊ฐ๋จํ์ง. ์ฌ๋ฌ ํ์ผ์ ์ ์กํ๊ณ ์ถ๋ค๋ฉด,
files
๋ณ์์ ์์์ ์ด๋ฆ์ผ๋ก ๋์ดํ๋ฉด ๋ผ.
์ค์!
ํ์ผ ์ ์ก ํ์๋ ํ์ผ์ ๋ซ์ ๋ฆฌ์์ค ๋์๋ฅผ ๋ฐฉ์งํด์ผ ํด.
with
๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ๋ฉด ์์
์ด ๋๋๋ฉด ์๋์ผ๋ก ํ์ผ์ด ๋ซํ:
import requests
with open('example.txt', 'rb') as f:
files = {'file': f}
response = requests.post('https://httpbin.org/post', files=files)
print(response.json())
4.4 ๋ก๊ทธ์ธ ๋ฐ ์ธ์ฆ
API (Application Programming Interface)๋ ์๋ก ๋ค๋ฅธ ํ๋ก๊ทธ๋จ๋ค์ด ์ํธ์์ฉ ํ ์ ์๊ฒ ํ๋ ๊ท์น๊ณผ ํ๋กํ ์ฝ์ ์งํฉ์ด์ผ. ๋ง์ ์น์ฌ์ดํธ์ ์๋น์ค๋ค์ด API์ ์ํธ์์ฉํ๊ธฐ ์ํด ๋ก๊ทธ์ธ ํ ์์ฒญ์ ๋ณด๋ด๋๋ก ํ์ฉํด.
์ฑ๊ณต์ ์ผ๋ก ๋ก๊ทธ์ธํ๋ฉด "์๋ฒ์์ ์ธ์ฆ ์ธ์
"์ ํฌํจํ๋ ์ธ์
(session)
๊ฐ์ฒด๋ฅผ ๋ฐ๊ฒ ๋ผ.
์ดํ ์์ฒญ์์๋ ์ด ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํด์ผ ํด.
์ธ์ฆ
์๋ฒ์ ๋ก๊ทธ์ธํ๊ธฐ ์ํด์๋ ์ธ์ฆ(๋ก๊ทธ์ธ ๊ณผ์ )์ ๊ฑฐ์ณ์ผ ํด, ์์ฒญ๊ณผ ํจ๊ป ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๋ณด๋ด์ผ ํด.
import requests
from requests.auth import HTTPBasicAuth
response = requests.get('https://httpbin.org/basic-auth/user/pass', \
auth=HTTPBasicAuth('user', 'pass'))
print(response.status_code)
์ด๊ฒ ์ธ์ฆ ๊ณผ์ ์ด์ผ, ํ์ง๋ง ์ผ๋ฐ์ ์ผ๋ก ์ธ์ ๊ณผ ํจ๊ป ์ฌ์ฉํด.
์ธ์ ์ฌ์ฉ
์ธ์
์ ์์ฒญ ๊ฐ์ cookies
๋ ์ฌ์ฉ์ ์ธ์ฆ ์ ๋ณด์ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ฅผ ์ ์งํ ์ ์์ด.
import requests
payload = {
'username': 'your_username',
'password': 'your_password'
}
# ์ธ์
์์ฑ
session = requests.Session()
# ์ฌ์ดํธ์ ๋ก๊ทธ์ธ
login_response = session.post('https://example.com/login', data = payload)
# ๋ก๊ทธ์ธ๋ ์ฌ์ฉ์๋ก ์ธ์
์์
์ ๊ณ์ ์งํ
data_response = session.get('https://example.com/api/data')
print(data_response.json())
GO TO FULL VERSION