-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_example.py
More file actions
25 lines (21 loc) · 841 Bytes
/
fetch_example.py
File metadata and controls
25 lines (21 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""fetch_example.py — fetch JSON from httpbin.org and print key info."""
import requests
def fetch():
url = "https://httpbin.org/get"
try:
r = requests.get(url, timeout=10) # 10 second timeout
print("Status:", r.status_code)
if r.status_code != 200:
print("Non-OK status, body preview:", r.text[:200])
return
data = r.json() # parse JSON to python dict
print("Origin (your IP):", data.get("origin"))
headers = data.get("headers", {})
print("User-Agent:", headers.get("User-Agent"))
print("All header keys:", list(headers.keys()))
except requests.Timeout:
print("Request timed out.")
except requests.RequestException as e:
print("Request failed:", e)
if __name__ == "__main__":
fetch()