如何使用 Python 在 Windows 中设置代理?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1201771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set proxy in Windows with Python?
提问by Esteban Küber
How can I get the current Windows' browser proxy setting, as well as set them to a value?
如何获取当前 Windows 的浏览器代理设置,并将它们设置为一个值?
I know I can do this by looking in the registry at Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyServer
, but I'm looking, if it is possible, to do this without messing directly with the registry.
我知道我可以通过查看注册表来做到这一点Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyServer
,但我正在寻找,如果可能的话,在不直接弄乱注册表的情况下做到这一点。
回答by Diaa Sami
urllib module automatically retrieves settings from registry when no proxies are specified as a parameter or in the environment variables
当没有指定代理作为参数或环境变量时,urllib 模块会自动从注册表中检索设置
In a Windows environment, if no proxy environment variables are set, proxy settings are obtained from the registry's Internet Settings section.
在 Windows 环境中,如果未设置代理环境变量,则从注册表的 Internet 设置部分获取代理设置。
See the documentation of urllib module referenced in the earlier post.
请参阅之前帖子中引用的 urllib 模块的文档。
To set the proxy I assume you'll need to use the pywin32 module and modify the registry directly.
要设置代理,我假设您需要使用 pywin32 模块并直接修改注册表。
回答by jkp
If the code you are using uses urlopen
under the hood you an set the http_proxy
environment variable to have it picked up.
如果您使用的代码urlopen
在幕后使用,您可以设置http_proxy
环境变量以获取它。
See the documentationhere for more info.
有关更多信息,请参阅此处的文档。
回答by PabloG
You can use WinHttpGetIEProxyConfigForCurrentUser as stated in this SO question
您可以使用 WinHttpGetIEProxyConfigForCurrentUser 如this SO question所述
There's another SO questionwith examples of PyWin32, the Python for Windows extensions.