【问题标题】:Change/Edit the proxy settings on windows using python使用 python 在 Windows 上更改/编辑代理设置
【发布时间】:2025-12-01 00:35:01
【问题描述】:

我正在尝试使用以下代码在 Windows 上使用 python 编辑/配置代理服务器。 但我得到一个错误。需要帮助!!

import 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', 1)
set_key('ProxyServer', u'192.168.0.5:3128')

【问题讨论】:

  • 我正在使用 Windows 8 我得到以下错误 >>> set_key('ProxyServer', u'192.168.0.5:3128') Traceback(最近一次调用最后):文件“”,第 1 行,在 文件 "",第 2 行,在 set_key FileNotFoundError: [WinError 2] The system cannot find the file specified
  • (没有足够的声望标记为重复)Change browser proxy settings from Python?
  • 我在我的用例中引用了上面相同的链接。但是,正如我报告的那样,代码的最后一行给出了错误。想摆脱这个谢谢
  • 我可以得到一些帮助来完成这项工作吗?

标签: windows


【解决方案1】:

您收到错误是因为您尝试查询尚不存在的键。您需要在注册表中创建密钥。 直接设置值也会创建键。

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

winreg.SetValueEx(INTERNET_SETTINGS, "ProxyServer", 0, winreg.REG_SZ, "your server address")

【讨论】: