Degang Guo
Degang Guo

Reputation: 505

How do I get the automatic proxy configuration for MacOS?

I have a Python script that needs to get the automatic agent configuration for MacOS and extract the agent address in the pac file. enter image description here

Is there a way for Python to get the URL in the image above?

Upvotes: 0

Views: 1297

Answers (2)

Pierz
Pierz

Reputation: 8168

It's possible to obtain the auto proxy url using the networksetup command (e.g. for Wi-Fi):

networksetup -getautoproxyurl Wi-Fi

Which can be obtained in Python:

import subprocess
auto_proxy_url=subprocess.run(['networksetup','-getautoproxyurl','Wi-Fi'], capture_output=True, text=True).stdout.split('\n')[0].split('URL:')[1].strip()

Upvotes: 0

Degang Guo
Degang Guo

Reputation: 505

After several hours of searching, we finally found a way to get the automatic agent configuration.

# -*- coding: utf-8 -*-
try:
    import SystemConfiguration
    config = SystemConfiguration.SCDynamicStoreCopyProxies(None)
    if all(('ProxyAutoConfigEnable' in config,'ProxyAutoConfigURLString' in config, not config.get('ProxyAutoDiscoveryEnable', 0))):
        print str(config['ProxyAutoConfigURLString'])
except Exception, e:
    print e

Upvotes: 2

Related Questions