[파이썬] requests 요청 재시도 로직 구현

In many cases, when making HTTP requests using the requests library in Python, it is important to handle situations where the request fails or times out. One common approach is to implement retry logic, which automatically retries the request a certain number of times before giving up.

In this blog post, we will explore how to implement retry logic for requests library in Python.

Installation

First, make sure you have the requests library installed. You can install it using pip:

pip install requests

Implementation

Here’s an example implementation of a retry mechanism for requests in Python:

import requests
from requests.exceptions import RequestException
import time

def make_request(url, retries=3, delay=1):
    """
    Makes an HTTP request to the given URL with retry logic.
    
    Args:
    - url (str): The URL to make the request to.
    - retries (int): The number of retries to attempt.
    - delay (int): The delay in seconds between retries.
    
    Returns:
    - response (requests.Response): The response object for the request.
    """
    for _ in range(retries):
        try:
            response = requests.get(url)
            response.raise_for_status()
            return response
        except (RequestException, ValueError) as e:
            print(f"Request failed: {str(e)}")
            time.sleep(delay)
    raise Exception(f"Failed to make request to {url} after {retries} retries.")

# Example usage
try:
    response = make_request("https://www.example.com/api")
    print(response.json())
except Exception as e:
    print(f"Error: {str(e)}")

In this example, the make_request function accepts the URL to make the request to, along with optional parameters for the number of retries and the delay between retries.

The function uses a loop to make the request and catches any exceptions that may occur. If the request is successful, the function returns the response object. If the request fails, it waits for the specified delay before retrying the request. If the maximum number of retries is reached, it raises an exception.

In the example usage, we call the make_request function with a sample URL (https://www.example.com/api). If the request is successful, we print the response data. If an error occurs, we print the error message.

Conclusion

Implementing retry logic in your HTTP requests can help to handle transient errors and improve the reliability of your code. By using the requests library in Python, you can easily implement this functionality. The example code provided in this blog post should serve as a starting point for your own retry logic implementation.

Remember to adjust the number of retries and the delay time based on the specific requirements of your application.

Happy coding!