PyObjC Framework NetworkExtension

12.1 · active · verified Tue Apr 14

PyObjC Framework NetworkExtension provides Python wrappers for Apple's NetworkExtension framework on macOS. It enables Python applications to interact with system-level networking features, such as VPN configuration and management. Currently at version 12.1, this library is part of the larger PyObjC project, which generally aligns its release cadence with new macOS SDK versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to list existing VPN configurations using `NEVPNManager`. It retrieves the shared manager instance, asynchronously loads all VPN preferences, and prints the names and enabled status of any found configurations. Note that proper entitlements and code signing are usually required for real-world usage of NetworkExtension APIs.

import NetworkExtension
import objc
from Foundation import NSLog
import threading

# Note: Accessing NetworkExtension APIs typically requires specific
# entitlements in your application's Info.plist and proper code signing.
# This script may require root privileges or fail without them.

def completion_handler(managers, error):
    if error:
        NSLog('Error loading VPN configurations: %@', error)
    elif managers:
        NSLog('Found %d VPN configurations:', len(managers))
        for manager in managers:
            NSLog('  - Name: %@, Enabled: %@', manager.localizedDescription(), 'Yes' if manager.enabled() else 'No')
    else:
        NSLog('No VPN configurations found.')
    
    # Signal that the async operation is complete
    completion_event.set()

NSLog('Attempting to load VPN configurations...')

# Use a threading.Event to wait for the asynchronous callback in a script
completion_event = threading.Event()

manager_class = NetworkExtension.NEVPNManager
manager = manager_class.sharedManager()

# loadAllFromPreferencesWithCompletionHandler_ is an asynchronous call.
# The Objective-C block is automatically translated to a Python callable.
manager.loadAllFromPreferencesWithCompletionHandler_(completion_handler)

# Wait for the completion handler to be called (max 5 seconds)
completion_event.wait(5.0)

NSLog('Quickstart finished.')

view raw JSON →