Project: Weather Data Fetcher
Project: Weather Data Fetcher
In this hands-on project, we will apply the HTTP networking concepts covered throughout this chapter—HTTP Requests, Query Parameters, JSON Parsing, and Network Exception Handling—to build a functional, real-time Command-Line Weather Data Fetcher.
We will connect to the open, free Open-Meteo API (which requires zero API keys or credit cards) to look up city coordinates and fetch current meteorological telemetry.
1. System Architecture & API Endpoints
Our weather application performs a two-stage API pipeline:
- 1Geocoding API: Resolves a user's textual city query (e.g.
"Mumbai"or"London") into exact latitude and longitude coordinates.
- Endpoint:
https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1
- 1Forecast Weather API: Fetches current weather telemetry for those coordinates.
- Endpoint:
https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lng}¤t_weather=true
- 1Data Translation Layer: Maps numerical WMO weather codes (e.g. Code
0= "Clear Sky", Code61= "Slight Rain") to user-friendly status descriptions.
2. Complete Application Code
Visual Architecture & Process Flow
How data and code flow step-by-step
3. Sample Execution Simulation
Multiple Choice Questions
1. In this project, what is the role of the Geocoding API step?
A. To check whether the user has internet access B. To translate the human-readable city string into decimal latitude and longitude coordinates C. To generate an SVG weather icon D. To verify user credentials Answer: B Explanation: The Weather forecast API requires numerical latitude and longitude coordinates, so the geocoding service translates city names like "London" into 51.51 and -0.13.
2. Why is self.session = requests.Session() instantiated inside the WeatherFetcher constructor?
A. To encrypt terminal output B. To reuse TCP connections across the geocoding and forecast queries, improving performance C. To prevent the program from running on Windows D. To disable DNS lookups Answer: B Explanation: requests.Session() reuses underlying network sockets (HTTP keep-alive) across both calls, reducing network handshake latency.
3. What does WEATHER_CODES.get(code, ("Unknown", "❓")) do if an unlisted code is encountered?
A. Raises an unhandled KeyError B. Returns the fallback tuple ("Unknown", "❓") safely without crashing C. Makes another network request D. Halts Python execution Answer: B Explanation: The .get() method on dictionaries allows providing a default fallback value when the searched key is not present.
4. How does the application prevent hanging if the remote weather API is unresponsive?
A. By setting a default timeout parameter on all requests B. By creating background threads C. By relying on the operating system to terminate the terminal D. By calling sys.exit() after every call Answer: A Explanation: Supplying timeout=self.timeout to requests.get() guarantees the socket will raise a requests.exceptions.Timeout exception if the server doesn't respond within the time limit.
5. What method is called on the response object to confirm whether the HTTP status code was successful?
A. response.verify_success() B. response.raise_for_status() C. response.confirm() D. response.assert_status() Answer: B Explanation: response.raise_for_status() checks the status code and raises an HTTPError if the response represents a client or server failure (4xx or 5xx).
Using print vs logging
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Practical API Example | Using print vs logging |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.