How to set proxy settings on MacOS using python
<p>How to change the internet proxy settings using python in MacOS to set <code>Proxy server</code> and <code>Proxy port</code></p>

<p>I do that with windows using this code:</p>

<pre><code>import _winreg as winreg

INTERNET_SETTINGS = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Internet Settings', 0, winreg.KEY_ALL_ACCESS)

def set_key(name, value):
    _, reg_type = winreg.QueryValueEx(INTERNET_SETTINGS, name)
    winreg.SetValueEx(INTERNET_SETTINGS, name, 0, reg_type, value)

set_key('ProxyEnable', 0)
set_key('ProxyOverride', u'*.local;&lt;local&gt;')  # Bypass the proxy for localhost
set_key('ProxyServer', u'proxy.example.com:8080')
</code></pre>

<p>is this possible to do it on MacOS ?</p>

<p>After a long time fo search, i found this way of how to change proxy on MacOs using python.</p>

<p>We need to use <code>networksetup</code> via terminal.</p>

<p>To set http proxy server on MacOS using python:</p>

<pre><code>import os

proxy = "proxy.example.com"
port = 8080

def Proxy_on():
    os.system('networksetup -setwebproxy Ethernet '+proxy+' '+port)

Proxy_on()
</code></pre>

<p>and to turn it off:</p>

<pre><code>import os

proxy = "proxy.example.com"
port = 8080

def Proxy_off():
    os.system('networksetup -setwebproxystate Ethernet off')

Proxy_off()
</code></pre>

<p>If the network service isn't named just <strong>"Ethernet"</strong>, you may need to parse <code>networksetup -listallnetworkservices</code> or <code>-listnetworkserviceorder</code> to get the correct name.</p>

python osx python-2.7 python-3.x proxy