【发布时间】:2011-08-04 03:14:02
【问题描述】:
我是 Python 新手,想编写一个脚本来根据我连接的网络更改 Windows 代理设置。我可以使用任何现有的 python 模块吗?感谢您的帮助。
谢谢, 瑟图
【问题讨论】:
我是 Python 新手,想编写一个脚本来根据我连接的网络更改 Windows 代理设置。我可以使用任何现有的 python 模块吗?感谢您的帮助。
谢谢, 瑟图
【问题讨论】:
我会使用winreg 并直接从the registry 查询设置。
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet
Settings] "MigrateProxy"=dword:00000001
"ProxyEnable"=dword:00000001
"ProxyHttp1.1"=dword:00000000
"ProxyServer"="http://ProxyServername:80"
"ProxyOverride"="<local>"
例如:
import _winreg
def getProxy():
proxy = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings")
server, type = _winreg.QueryValueEx(proxy, "ProxyServer")
enabled, type = _winreg.QueryValueEx(proxy, "ProxyEnable")
if enabled:
return server
return None
【讨论】:
您不能在发送请求之前在 Windows 中(手动或在您的程序中)为您的应用程序设置 HTTP_PROXY 环境变量吗?这应该注意您通过 urllib2 发送的任何请求都通过代理。
【讨论】: