[파이썬] requests 연결 유지하기 (Keep-Alive)

When making HTTP requests in Python, the requests library is a popular choice. It provides a simple and elegant way to send requests and handle responses. One important feature of HTTP requests is the ability to keep the connection alive, also known as Keep-Alive.

By default, requests library doesn’t enable Keep-Alive, which means that a new connection is created for every request. However, enabling Keep-Alive can have significant performance benefits as it allows multiple requests to be sent over the same connection, reducing the overhead of establishing a new connection for every request.

Enabling Keep-Alive in requests

To enable Keep-Alive in your requests, you can set the Connection header to keep-alive using the headers parameter. Here’s an example:

import requests

url = "https://www.example.com/api"
headers = {
    'Connection': 'keep-alive'
}

response = requests.get(url, headers=headers)

print(response.status_code)

In the example above, we are sending a GET request to "https://www.example.com/api" with the Connection header set to keep-alive. This tells the server to keep the connection open for future requests.

Connection Pooling

Another advantage of enabling Keep-Alive is that it enables Connection Pooling. This means that requests made to the same server will reuse existing connections from a pool, rather than creating new connections.

Requests library has built-in connection pooling support, so enabling Keep-Alive will automatically enable connection pooling as well.

Here’s an example of using a connection pool with Keep-Alive:

import requests

# Create a session object with connection pooling
session = requests.Session()
session.headers['Connection'] = 'keep-alive'

# Send multiple requests using the same session
for i in range(5):
    response = session.get("https://www.example.com/api")
    print(response.status_code)

In the above example, we create a Session object which handles connection pooling. We set the Connection header to keep-alive on the session object. Then, we can send multiple requests using the same session, and they will reuse the same connection from the pool.

Conclusion

Enabling Keep-Alive in your Python requests can result in improved performance and efficiency by reusing connections and reducing connection-establishment overhead. By setting the Connection header to keep-alive and using connection pooling, you can enhance the performance of your HTTP requests. Don’t forget to handle potential errors or timeouts that may occur when using Keep-Alive, and always test the performance gain in your specific use case.