Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In modern software development, applications rarely operate in isolation. They communicate with external web services, payment gateways, authentication providers, and cloud databases via HTTP (Hypertext Transfer Protocol). While Python includes a standard urllib module, the third-party requests library is the undisputed industry standard—dubbed "HTTP for Humans"—due to its intuitive, developer-friendly API.
HTTP follows a classic client-server model:
GET, POST), target URL, optional Headers (metadata), and optional Body payload.200 OK: Request succeeded.201 Created: Resource successfully created.400 Bad Request: Malformed client payload.401 Unauthorized: Authentication credentials missing or invalid.403 Forbidden: Authenticated, but lacking permission.404 Not Found: Target endpoint does not exist.500 Internal Server Error: Server-side crash.503 Service Unavailable: Server overloaded or undergoing maintenance.Install the library using pip:
Making a basic GET request:
Response Object| Attribute / Method | Description | Example |
|---|---|---|
response.status_code | Integer HTTP status code | 200, 404 |
response.ok | Boolean: True if status code < 400 | True |
response.text | Decoded string content (uses encoding) | "{\"title\": \"...\"}" |
response.content | Raw binary bytes (for images/files) | b'\x89PNG...' |
response.headers | Case-insensitive dictionary of HTTP headers | {'Date': '...', ...} |
response.url | Final URL after any redirects | "https://..." |
response.raise_for_status() | Raises HTTPError if status is 4xx or 5xx | Exception or None |
By default, requests does not time out. If an external server freezes or hangs, your Python script will wait indefinitely, blocking execution and consuming server threads.
Always pass the timeout parameter (in seconds):
requests.exceptionsAll exceptions in the requests library inherit from requests.exceptions.RequestException. Handle network errors defensively:
timeout on Network CallsBy default, requests.get() will wait indefinitely if a remote server hangs. Always provide an explicit timeout: requests.get(url, timeout=5.0).
response.raise_for_status()A response with HTTP 404 (Not Found) or 500 (Server Error) does not raise an exception automatically. Always call response.raise_for_status() to catch HTTP errors reliably.
Use the requests library to query GitHub's public API for a user profile (https://api.github.com/users/<username>), verifying status codes and displaying the public repository count.
response.ok property return when a server responds with status code 200?requests.get() call without specifying a timeout argument and the remote server never responds?HTTPError if the response status indicates an error (4xx or 5xx)?response.text and response.content?requests library?In the next lesson, we will continue your intermediate Python journey with GET & POST Methods (8: Working with APIs).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Project: Infinite Sequence Generator | GET & POST Methods |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.