gpt4 book ai didi

python - 通过 python 更改注册表工作不正确

转载 作者:行者123 更新时间:2023-11-28 22:47:56 26 4
gpt4 key购买 nike

import _winreg as registry
key=registry.OpenKey(registry.HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Internet Settings",0,registry.KEY_ALL_ACCESS)
proxy=proxy_server+":"+proxy_port
registry.SetValue(key, 'ProxyEnable', registry.REG_SZ, 'dword:00000001')
registry.SetValue(key, 'ProxyServer', registry.REG_SZ, proxy)

我正在使用上面的代码设置代理。但它不会在互联网设置中添加新 key 。而是创建一个新文件夹并使用提供的值放入名为 default 的 key 。

enter image description here

谁能帮我解决这个问题。真的卡住了

最佳答案

虽然我不明白为什么您的代码不起作用,但我能够重现该问题并想出一个解决方法——即通过_winreg.SetValueEx() 函数。

具有该微小更改的代码如下所示。我还向 key 字符串常量添加了一个 r 前缀,因为它包含文字反斜杠字符(但这不是导致问题的原因)。 (请注意,我还更改了一些文字的名称和值,以免与我自己系统的注册表中可能存在的任何内容发生冲突。)

import _winreg as registry

proxy_server = 'some_proxy_server' # dummy value
proxy_port = 'some_proxy_port' # dummy value

key = registry.OpenKey(registry.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Internet Settings",
0, registry.KEY_ALL_ACCESS)
proxy = proxy_server + ":" + proxy_port

# SetValue doesn't work for some reason...
#registry.SetValue(key, '_ProxyEnable', registry.REG_SZ, 'dword:00000001')
#registry.SetValue(key, '_ProxyServer', registry.REG_SZ, proxy)

# but SetValueEx does work
registry.SetValueEx(key, "_ProxyEnable", 0, registry.REG_SZ, "dword:00000001")
registry.SetValueEx(key, "_ProxyServer", 0, registry.REG_SZ, proxy)

我还必须评论将 ProxyEnable 设置为字符串值 "dword:00000001" 而不是 REG_DWORD 1 似乎有点奇怪...但这是您的代码试图执行的操作。

screenshot showing properly changed value in registry editor

关于python - 通过 python 更改注册表工作不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25669169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com