[파이썬] requests 내부 동작 방식

In the world of web development, making HTTP requests is a common task. Python provides a powerful library called requests that simplifies the process of sending HTTP requests and handling the responses. In this blog post, we will dive into the inner workings of requests and explore how it functions under the hood.

The Anatomy of a Request

Before we delve into the inner workings of requests, let’s review the key components of an HTTP request:

  1. URL - The Uniform Resource Locator (URL) specifies the location of a resource on the web.
  2. Method - The HTTP method indicates the desired action to be performed on the resource. Common methods include GET, POST, PUT, PATCH, and DELETE.
  3. Headers - HTTP headers contain additional information about the request, such as the content type or authorization credentials.
  4. Data - In some cases, requests may include data to be sent to the server, such as form data or JSON payloads.

The Basics of requests

Now, let’s discuss how requests handles the process of making HTTP requests. requests is a simplified and user-friendly library that abstracts away the complexities of the underlying network protocols. It makes interacting with web services a breeze.

Here’s a basic example of using requests to send a GET request:

import requests

response = requests.get('https://api.example.com/users')
print(response.status_code)
print(response.json())

In the example above, we import the requests module and use the get() function to send a GET request to https://api.example.com/users. The response object contains various attributes and methods, such as status_code to check the HTTP status code and json() to retrieve the response as JSON.

Under the Hood

Underneath its user-friendly interface, requests relies on other Python libraries to handle the low-level details of sending and receiving HTTP requests. It uses urllib3 for connection pooling and certifi for handling SSL certificates.

When you issue a request with requests, the library performs the following steps:

  1. URL Parsing and Validation - requests parses the given URL, checks its validity, and separates the scheme, hostname, port, and path components.
  2. Connection Pooling - requests takes advantage of connection pooling with urllib3 to improve performance and efficiency by reusing existing TCP connections whenever possible.
  3. Request Preparation - requests prepares the request by assembling the necessary headers, data payloads, and authentication information.
  4. Request Dispatch - requests dispatches the prepared request by establishing a connection to the server and sending the necessary HTTP commands.
  5. Response Processing - Once the server responds, requests handles the response by interpreting the status code, headers, and content.
  6. Error Handling - requests handles various types of errors, including network errors, timeouts, and invalid responses.

Conclusion

Understanding the inner workings of requests allows us to appreciate its power and simplicity when it comes to making HTTP requests. By abstracting away the complexities, requests enables Python developers to focus on building robust and scalable web applications without getting bogged down in the nitty-gritty details of network protocols.

So, next time you need to interact with a web service in Python, remember the magic of requests and how it streamlines the process of making HTTP requests!