Adafruit CircuitPython ConnectionManager

3.1.7 · active · verified Fri Apr 17

Adafruit CircuitPython ConnectionManager (current version 3.1.7) provides a `urllib3.poolmanager`/`urllib3.connectionpool`-like interface for managing network sockets and connections, primarily on CircuitPython devices. It abstracts the underlying socket pool, making it easier to integrate with higher-level libraries like `adafruit-circuitpython-requests`. Releases occur periodically, often in response to bug fixes, dependency updates, or CPython compatibility improvements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `ConnectionManager` with a `SocketPool` and `ssl` context, then integrate it with `adafruit-circuitpython-requests` to perform an HTTP(S) GET request. This example is runnable on CPython after installing `socketpool`, `adafruit-circuitpython-connectionmanager`, and `adafruit-circuitpython-requests`.

import os
import socketpool
import adafruit_connection_manager
import adafruit_requests as requests
import ssl # Required for HTTPS

# 1. Initialize a SocketPool.
# For CPython, use `pip install socketpool`.
# For CircuitPython, this would be provided by your board's firmware or a driver
# like `adafruit_esp32spi.adafruit_esp32spi_socket` or `adafruit_wiznet5k.adafruit_wiznet5k_socket`.
pool = socketpool.SocketPool()

# 2. Create an SSL context for HTTPS requests.
# This is crucial for secure connections.
ssl_context = ssl.create_default_context()

# 3. Instantiate ConnectionManager, passing the socket pool and SSL context.
manager = adafruit_connection_manager.ConnectionManager(pool, ssl_context)

# 4. Set ConnectionManager as the socket interface for adafruit_requests.
# For CPython, use `pip install adafruit-circuitpython-requests`.
requests.set_socket(manager)

# 5. Make an HTTP(S) request.
# Using os.environ.get for a safe default URL.
test_url = os.environ.get("TEST_URL", "https://httpbin.org/get")
print(f"Attempting to fetch data from: {test_url}")

try:
    response = requests.get(test_url)
    print(f"Status Code: {response.status_code}")
    print(f"Response (first 100 chars): {response.text[:100]}...")
    response.close() # Important to close responses in CircuitPython
except Exception as e:
    print(f"Error fetching URL: {e}")

view raw JSON →