How to set proxy in Windows with Python?

梦想的初衷 提交于 2019-12-24 06:59:10

问题


How can I get the current Windows' browser proxy setting, as well as set them to a value?

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.


回答1:


urllib module automatically retrieves settings from registry when no proxies are specified as a parameter or in the environment variables

In a Windows environment, if no proxy environment variables are set, proxy settings are obtained from the registry’s Internet Settings section.

See the documentation of urllib module referenced in the earlier post.

To set the proxy I assume you'll need to use the pywin32 module and modify the registry directly.




回答2:


If the code you are using uses urlopen under the hood you an set the http_proxy environment variable to have it picked up.

See the documentation here for more info.




回答3:


You can use WinHttpGetIEProxyConfigForCurrentUser as stated in this SO question

There's another SO question with examples of PyWin32, the Python for Windows extensions.



来源:https://stackoverflow.com/questions/1201771/how-to-set-proxy-in-windows-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!