PyObjC CFNetwork Framework Bindings

12.1 · active · verified Tue Apr 14

PyObjC provides Python bindings for Apple's macOS frameworks, allowing Python applications to interact with Objective-C APIs. The `pyobjc-framework-cfnetwork` package specifically provides wrappers for the CFNetwork framework, which offers a C-based API for low-level network services. It is actively maintained as part of the broader PyObjC project, with releases often coinciding with new macOS SDK updates and Python version support. The current version is 12.1.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `CFNetwork.CFHostCreateWithName` to create a host object and then resolve its IP addresses. It showcases basic interaction with a C-based macOS networking API through PyObjC.

import CFNetwork

# Create a CFHost object for a given hostname
hostname = "www.apple.com"
host = CFNetwork.CFHostCreateWithName(None, hostname)

if host:
    # Start info resolution (blocking for simplicity; real apps might use async)
    # kCFHostAddresses resolves IP addresses
    success = CFNetwork.CFHostStartInfoResolution(
        host, CFNetwork.kCFHostAddresses, None
    )
    if success:
        # Retrieve the resolved addresses
        # PyObjC automatically converts CFArrayRef to Python list
        addresses = CFNetwork.CFHostGetAddressing(host, None)
        if addresses:
            print(f"Resolved addresses for {hostname}:")
            for address in addresses:
                # address is a CFDataRef containing sockaddr_storage
                print(f"  {address}") 
        else:
            print(f"No addresses found for {hostname}")
    else:
        print(f"Failed to resolve host {hostname}")
    
    # Stop resolution and release resources (PyObjC handles Python object lifecycle)
    CFNetwork.CFHostStopInfoResolution(host)
else:
    print(f"Failed to create CFHost object for {hostname}")

view raw JSON →