urllib3-future: HTTP/1.1, 2, and 3 Client (Sync & Async)

2.19.905 · active · verified Sat Apr 11

urllib3.future is a powerful HTTP 1.1, 2, and 3 client with both synchronous and asynchronous interfaces, designed to be forward-looking. It provides a robust alternative to the standard urllib3 library, offering support for modern HTTP protocols and optional memory-safe TLS backends like Rustls. The project is under active and rapid development, with frequent releases.

Warnings

Install

Imports

Quickstart

Demonstrates both asynchronous and synchronous usage patterns with `AsyncConnectionPool` and `ConnectionPool` respectively, fetching data from `httpbin.org`. The async example is run via `asyncio.run()`, and the sync example is called directly.

import urllib3.future as urllib3
import asyncio
import os

# Asynchronous example
async def async_example():
    # No authentication needed for httpbin.org
    pool = urllib3.AsyncConnectionPool("https://httpbin.org")
    try:
        print("\n--- Async Example ---")
        resp = await pool.request("GET", "/get")
        print(f"Status: {resp.status}")
        # Decode data and truncate for cleaner output
        print(f"Data: {resp.data.decode('utf-8')[:200]}...")
    except Exception as e:
        print(f"Async request failed: {e}")
    finally:
        await pool.close()

# Synchronous example
def sync_example():
    pool = urllib3.ConnectionPool("https://httpbin.org")
    try:
        print("\n--- Sync Example ---")
        resp = pool.request("GET", "/get")
        print(f"Status: {resp.status}")
        print(f"Data: {resp.data.decode('utf-8')[:200]}...")
    except Exception as e:
        print(f"Sync request failed: {e}")
    finally:
        pool.close()

# Run the examples
if __name__ == "__main__":
    asyncio.run(async_example())
    sync_example() # Synchronous call, not within async loop

view raw JSON →