如何使用python在MacOS上设置代理设置 [英] How to set proxy settings on MacOS using python
问题描述
如何在MacOS中使用python更改Internet代理设置以设置Proxy server
和Proxy port
How to change the internet proxy settings using python in MacOS to set Proxy server
and Proxy port
我使用以下代码在Windows中执行此操作:
I do that with windows using this 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;<local>') # Bypass the proxy for localhost
set_key('ProxyServer', u'proxy.example.com:8080')
在MacOS上可以这样做吗?
is this possible to do it on MacOS ?
推荐答案
经过长时间的搜索,我发现了这种使用python在MacO上更改代理的方法.
After a long time of search, I found this way of how to change proxy on MacOs using python.
我们需要通过终端使用networksetup
.
We need to use networksetup
via terminal.
要使用python在MacOS上设置HTTP代理服务器:
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()
并关闭它:
import os
proxy = "proxy.example.com"
port = 8080
def Proxy_off():
os.system('networksetup -setwebproxystate Ethernet off')
Proxy_off()
如果网络服务的名称不只是以太网" ,则可能需要解析networksetup -listallnetworkservices
或-listnetworkserviceorder
以获得正确的名称.
If the network service isn't named just "Ethernet", you may need to parse networksetup -listallnetworkservices
or -listnetworkserviceorder
to get the correct name.
这篇关于如何使用python在MacOS上设置代理设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!