Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python在MacOS上设置代理设置_Python_Macos_Python 2.7_Python 3.x_Proxy - Fatal编程技术网

如何使用python在MacOS上设置代理设置

如何使用python在MacOS上设置代理设置,python,macos,python-2.7,python-3.x,proxy,Python,Macos,Python 2.7,Python 3.x,Proxy,如何在MacOS中使用python更改internet代理设置以设置代理服务器和代理端口 我使用以下代码对windows执行此操作: 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 s

如何在MacOS中使用python更改internet代理设置以设置
代理服务器
代理端口

我使用以下代码对windows执行此操作:

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')
导入\u winreg作为winreg
INTERNET\u SETTINGS=winreg.OpenKey(winreg.HKEY\u当前用户,r'Software\Microsoft\Windows\CurrentVersion\INTERNET SETTINGS',0,winreg.KEY\u全部访问)
def set_键(名称、值):
_,reg_type=winreg.queryvaluex(INTERNET_设置,名称)
winreg.SetValueEx(互联网设置、名称、0、注册类型、值)
设置密钥(“代理启用”,0)
设置_key('ProxyOverride',u'*.local;')#绕过本地主机的代理
set_key('ProxyServer',u'proxy.example.com:8080'))

这可以在MacOS上实现吗?

经过长时间的搜索,我找到了使用python在MacOS上更改代理的方法

我们需要通过终端使用
networksetup

要使用python在MacOS上设置HTTP代理服务器,请执行以下操作:

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()
如果网络服务并非仅命名为“Ethernet”,则可能需要解析
网络设置-listalNetworkServices
-listnetworkserviceorder
,以获得正确的名称