Python HTTPクライアント¶
ライブラリについて¶
HTTPプロトコルを使用したHTTPクライアント用のライブラリとして、次があります。(他にもあるが著名なもののみ記載)
- Python 3標準のhttp.client
- Python 3標準のurllib.request
内部でhttp.clientを使用した高水準API、非同期通信なし、multipart/form-data対応なし - Requests
簡潔・直感的にコードを記述できるAPI、非同期通信なし - HTTPX
非同期通信、HTTP/2対応したAPI
ライブラリ利用例¶
urllib.request¶
from urllib.request import urlopen
import json
with urlopen("https://foo.example.com/api") as resp:
data = json.load(resp)
print(f'{data["current_user_url"]}')
Requests¶
import requests
resp = requests.get("https://foo.example.com/api")
print(f'{resp.json()["current_user_url"]}')