Networking, HTTP and APIs
The web runs on the HTTP protocol: a client sends a request (GET to read, POST to send data…) to a URL, and the server sends back a response with a status code (200 = OK, 404 = not found, 500 = server error) and content, often in JSON.
An API is a service that answers these requests with data: weather, maps, payments… In Python, you use the requests package: requests.get(url).json(). This browser blocks requests to other sites, so here you practise what comes before and after: building the URL with urlencode(), then reading the JSON response.
import json
from urllib.parse import urlencode
base = "https://api.example.com/weather"
params = {"city": "Paris", "units": "metric"}
url = base + "?" + urlencode(params)
print(url)
# On your computer: data = requests.get(url).json()
response_text = '{"city": "Paris", "temp": 12, "status": "ok"}'
data = json.loads(response_text)
print(data["city"], data["temp"], "°C")