geventhttpclient
geventhttpclient is an asynchronous HTTP client library specifically designed to work with gevent, providing non-blocking HTTP requests. It is currently at version 2.3.9 and has a moderate release cadence, with updates typically several times a year as needed for bug fixes and compatibility.
Warnings
- gotcha gevent's `monkey.patch_all()` is essential. If not called, `geventhttpclient` will perform blocking I/O operations, defeating the purpose of gevent.
- gotcha Always close `HTTPClient` instances explicitly (`client.close()`) or use them as context managers (`with HTTPClient(...) as client:`). Failing to do so can lead to connection leaks and resource exhaustion, especially when using connection pooling.
- gotcha By default, `geventhttpclient` does not raise exceptions for HTTP error status codes (e.g., 4xx, 5xx). You must explicitly check `response.status` or configure the client to raise exceptions.
- gotcha Reading the entire response body with `response.read()` loads it into memory. For very large responses, this can consume significant memory.
Install
-
pip install geventhttpclient
Imports
- HTTPClient
from geventhttpclient import HTTPClient
- HTTPClientPool
from geventhttpclient.pool import HTTPClientPool
- response
from geventhttpclient import response
Quickstart
import gevent
from geventhttpclient import HTTPClient
from gevent import monkey
import logging
# Crucial for gevent to work properly
monkey.patch_all()
# Optional: Set up basic logging to see gevent events if needed
# logging.basicConfig(level=logging.INFO)
def fetch_url(url):
# Create a client instance for a specific host
# Using httpbin.org for a public, testable endpoint
client = HTTPClient.from_url(url,
connection_timeout=5,
network_timeout=10)
try:
# Make a GET request to a path relative to the client's base URL
resp = client.get('/get?foo=bar')
print(f"Requested: {url}/get?foo=bar")
print(f"Status: {resp.status}")
print(f"Headers: {resp.headers['Content-Type']}")
# Read the response body. For large responses, consider resp.read(chunk_size)
body = resp.read()
print(f"Body snippet: {body[:100]}...")
except Exception as e:
print(f"Error fetching {url}: {e}")
finally:
# Always close the client to release resources, especially connection pool
client.close()
if __name__ == '__main__':
# Spawn a greenlet to run the network operation non-blocking
greenlet = gevent.spawn(fetch_url, 'http://httpbin.org')
# Wait for the greenlet to complete
greenlet.join()