Take two (attached).

BTW, I'm not sure if I'm doing the memory management correctly, especially with the values returned from CFDictionaryGetValue (it might need a CFRelease in there). Could someone double-check me on that please?

~ Daniel


def getproxies_internetconfig():
    """Return a dictionary of scheme -> proxy server URL mappings.

    By convention the mac uses Internet Config to store
    proxies.  An HTTP proxy, for instance, is stored under
    the HttpProxy key.

    """
    from ctypes import cdll, byref, c_void_p, c_int, c_long, create_string_buffer
    from ctypes.util import find_library

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

    def create_cfstringref(cstr):
        return sc.CFStringCreateWithCString(0, cstr, 0)

    kCFNumberSInt32Type = 3
    kSCPropNetProxiesHTTPEnable = create_cfstringref("HTTPEnable")
    kSCPropNetProxiesHTTPProxy = create_cfstringref("HTTPProxy")
    kSCPropNetProxiesHTTPPort = create_cfstringref("HTTPPort")
    
    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

    proxies = {}
    proxyDict = sc.SCDynamicStoreCopyProxies(None)
    try:
        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)
    finally:
        sc.CFRelease(proxyDict)

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

Reply via email to