Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Make GET requests
requests.get returns a response with status, headers and the body (often JSON).
import requests
r = requests.get("https://api.github.com/repos/s0sta/it-notes", timeout=10)
r.raise_for_status()
repo = r.json()
print(repo["full_name"], repo["stargazers_count"], repo["updated_at"])
r = requests.get("https://s0sta.com/", timeout=10)
print(r.status_code, r.elapsed.total_seconds(), r.headers.get("server"))Onthoud
Always set a timeout. Without it, a script can hang forever on a dead server.
Send data and use tokens
Send JSON with json=; put the token in a header from an environment variable — never in the code.
import os
import requests
token = os.environ["GITHUB_TOKEN"]
headers = {"Authorization": f"Bearer {token}", "Accept": "application/vnd.github+json"}
issue = {"title": "Disk on web1 above 90%", "labels": ["ops"]}
r = requests.post("https://api.github.com/repos/s0sta/infra-shop/issues", json=issue, headers=headers, timeout=10)
r.raise_for_status()
print("created", r.json()["html_url"])Handle pages
APIs return big lists in pages. Follow the next link (or page number) until the end.
import os
import requests
url = "https://api.github.com/orgs/github/repos?per_page=100"
headers = {"Authorization": f"Bearer {os.environ['GITHUB_TOKEN']}"}
names = []
while url:
r = requests.get(url, headers=headers, timeout=10)
r.raise_for_status()
names += [repo["name"] for repo in r.json()]
url = r.links.get("next", {}).get("url")
print(len(names))Retry on errors
Networks fail sometimes. Retry with a growing wait for temporary errors (429, 5xx).
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
session = requests.Session()
retry = Retry(total=5, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504], allowed_methods=["GET", "POST"])
session.mount("https://", HTTPAdapter(max_retries=retry))
r = session.get("https://api.example.com/health", timeout=5)
print(r.status_code)Check many websites
A small uptime checker: status, time and certificate per site.
import requests
sites = ["https://s0sta.com/", "https://web3.s0sta.com/", "https://babolatksa.com/"]
for url in sites:
try:
r = requests.get(url, timeout=10)
state = "UP" if r.ok else f"HTTP {r.status_code}"
print(f"{state:8} {r.elapsed.total_seconds():5.2f}s {url}")
except requests.RequestException as e:
print(f"DOWN {url} ({e.__class__.__name__})")Op het werk
Use the official SDKs where they exist (azure-identity + azure-mgmt-*, kubernetes, PyGithub) — they handle login, paging and retries for you.