geventhttpclient

2.3.9 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize gevent's monkey patching, create an `HTTPClient` instance, perform a GET request, and handle the response. It highlights the importance of `monkey.patch_all()` and proper client closure.

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()

view raw JSON →