Lesson 5 of 20 · 52 min · UG / professional
HTTP, dates and compressed files
A weather JSON body is labelled fields plus failure modes (timeout, 404). Dates need a timezone the college actually uses. zip/gzip are how last year’s logs ship.
Telemetry packet
Nested keys like an API payload.
You will be able to
- ✓ Treat a weather JSON body as labelled fields plus named failures
- ✓ Pass ‘today’ in so tests do not wait for midnight
- ✓ Know zip/gzip are how last year’s gate logs ship
HTTP is a dict with extra failure modes
The campus weather station returns JSON: temp, humid, maybe a station id. On a laptop, requests (or urllib) fetches it. A timeout, a 404 and a 500 are not fields — they are failures you must name. Production clients set a timeout. Never assume the network returned a dict; check, then read.
This sandbox dict is the happy path so ["temp"] is not your first time. Copy the sample: timeout=5, then .json(). Asia/Kolkata is the timezone the college actually uses — not the default of a cloud VM in another country.
Dates at the edge; compression for last year
Stamp a report with a date string you passed in. Tests freeze "2026-08-24". Do not bury datetime.now() inside a GST function.
zip and gzip are how last monsoon’s logs leave the Pi. You do not unzip in this Run box. You do keep the packed filename in CONFIG so a laptop open(..., encoding="utf-8") knows which file to stream.
JSON keys are the contract
payload["temp"] after a named OK is the same skill as a CSV column. Missing keys are a policy: default, or Reject — not a surprise crash at 2 a.m.
Words that matter
- JSON
- Text that maps to dicts and lists — the usual HTTP body.
- Timeout
- How long you are willing to wait for a network; always set one.
- gzip / zip
- Compressed archives — last year’s logs, not a live kiosk feed.
Common mistakes
Avoid: payload["temp"] with no check that the request succeeded.
Do this: Named failure for timeout/404; then read fields.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — weather-shaped payload
Same keys as JSON. Guard a missing OK flag.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — a date you can pass in a test
No clock in the function. Packed log name lives in CONFIG.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
On a full Python install — requests, datetime, gzip
Campus weather JSON with a timeout; Kolkata calendar stamp; last year’s logs as gzip text.
datetime/gzip: stdlib. requests: pip install requests
Timeout on the client. Date passed or timezone-aware. gzip for archives.
Real library code (not run in this browser sandbox)
from datetime import date
import gzip
import requests
def weather():
r = requests.get("https://example.invalid/campus-weather", timeout=5)
r.raise_for_status()
return r.json()
def stamp(day):
return day.isoformat()
# gzip.open("gate-2025.json.gz", "rt", encoding="utf-8")
print("would stamp", stamp(date(2026, 8, 24)))
print("would GET with timeout 5")
Example program — Timeout path, then a field
ok 0 is the named failure. Happy path still reads temp.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Read temp
body = {"temp": 29, "ok": 1}. Print so 29 appears.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.