Try again...that last file was my scratch file I was using for development. Here's the real thing. Sorry for the mess.

~ Daniel


def getproxies_internetconfig():
    """Return a dictionary of scheme -> proxy server URL mappings.
    
    This function has 'internetconfig' in its name for historical reasons only.
    It uses the more modern 'SystemConfiguration' library to obtain its result.
    """
    from ctypes import cdll, byref, c_int, create_string_buffer
    from ctypes.util import find_library

    sc = cdll.LoadLibrary(find_library("SystemConfiguration"))
    
    if not sc:
        return {}

    def CStringFromCFString(value):
        length = sc.CFStringGetLength(value) + 1
        buff = create_string_buffer(length)
        sc.CFStringGetCString(value, buff, length, 0)
        return buff.value

    def CFNumberToInt32(cfnum):
        val = c_int()
        sc.CFNumberGetValue(cfnum, kCFNumberSInt32Type, byref(val))
        return val.value

    kCFNumberSInt32Type = 3
    kSCPropNetProxiesHTTPEnable = sc.CFStringCreateWithCString(0, "HTTPEnable", 0)
    kSCPropNetProxiesHTTPProxy = sc.CFStringCreateWithCString(0, "HTTPProxy", 0)
    kSCPropNetProxiesHTTPPort = sc.CFStringCreateWithCString(0, "HTTPPort", 0)

    proxies = {}
    proxyDict = sc.SCDynamicStoreCopyProxies(None)
    try:
        # HTTP:
        enabled = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesHTTPEnable)
        if sc.CFBooleanGetValue(enabled):
            proxy = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesHTTPProxy)
            proxy = CStringFromCFString(proxy)
            port = sc.CFDictionaryGetValue(proxyDict, kSCPropNetProxiesHTTPPort)
            port = CFNumberToInt32(port)
            proxies["http"] = "http://%s:%i"; % (proxy, port)

        # FTP: XXXX To be done.
        # Gopher: XXXX To be done.
    finally:
        sc.CFRelease(proxyDict)

    sc.CFRelease(kSCPropNetProxiesHTTPEnable)
    sc.CFRelease(kSCPropNetProxiesHTTPProxy)
    sc.CFRelease(kSCPropNetProxiesHTTPPort)

    return proxies
_______________________________________________
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig

Reply via email to