How to set proxy settings on MacOS using python

后端 未结 1 1515
长情又很酷
长情又很酷 2021-01-21 13:03

How to change the internet proxy settings using python in MacOS to set Proxy server and Proxy port

I do that with windows using this code:

相关标签:
1条回答
  • 2021-01-21 13:37

    After a long time of search, I found this way of how to change proxy on MacOs using python.

    We need to use networksetup via terminal.

    To set HTTP proxy server on MacOS using python:

    import os
    
    proxy = "proxy.example.com"
    port = 8080
    
    def Proxy_on():
        os.system('networksetup -setwebproxy Ethernet '+proxy+' '+port)
    
    Proxy_on()
    

    and to turn it off:

    import os
    
    proxy = "proxy.example.com"
    port = 8080
    
    def Proxy_off():
        os.system('networksetup -setwebproxystate Ethernet off')
    
    Proxy_off()
    

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

    0 讨论(0)
提交回复
热议问题