好吧,代码显示不太正常的样子,pastbin吧。
import ctypes
import _winreg
# See http://msdn.microsoft.com/en-us/library/aa385328(v=vs.85).aspx
#: Causes the proxy data to be reread from the registry for a handle. No buffer is required. This option can be used on the HINTERNET handle returned by InternetOpen. It is used by InternetSetOption.
INTERNET_OPTION_REFRESH = 37
#: Notifies the system that the registry settings have been changed so that it verifies the settings on the next call to InternetConnect. This is used by InternetSetOption.
INTERNET_OPTION_SETTINGS_CHANGED = 39
# setup function
Wininet = ctypes.windll.Wininet
InternetSetOption = Wininet.InternetSetOptionW
HKEY_CURRENT_USER = _winreg.HKEY_CURRENT_USER
SUB_KEY_PATH = r'Software\Microsoft\Windows\CurrentVersion\Internet Settings'
def main():
hkey = _winreg.OpenKey(HKEY_CURRENT_USER, SUB_KEY_PATH, 0, _winreg.KEY_READ | _winreg.KEY_WRITE)
(ProxyEnabled, _) = _winreg.QueryValueEx(hkey, 'ProxyEnable')
_winreg.SetValueEx(hkey, 'ProxyEnable', 0, _winreg.REG_DWORD, not ProxyEnabled)
print 'enabled' if not ProxyEnabled else 'disabled'
InternetSetOption(0, INTERNET_OPTION_REFRESH, 0, 0)
InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
if __name__ == '__main__':
main()程序首先检查
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable的值,然后Toggle一下,0则设为1,1则设为0。然后通过InternetSetOption函数告知系统设置已变。Chrome不自备代理控制功能,感觉Swichy的体验不好。之前尝试用显示的注册表操作命令完成,可是Chrome硬是不随之变化,非得再开一次那个Internet Options窗口才生效。特别是更新.pac文件的时候,真苦逼,居然假死。
想要接触Win32和Python的C扩展,希望对系统和编译器了解得多一些,但是Visual C++和Win32的那些宏真的很繁琐,即使用Python也不容易放开手脚。
更新:
[1]. 用ctypes调用RegOpenKeyEx和RegQueryKeyEx,http://pastebin.com/35zv649Q
[2]. 第一次写Windows API程序。真复杂。http://pastebin.com/kGWGJMmL
有用的链接:
[1]. InternetSetOption Function,http://msdn.microsoft.com/en-us/library/aa385114(v=vs.85).aspx
[2]. Option Flags,http://msdn.microsoft.com/en-us/library/aa385328(v=vs.85).aspx
[3]. The ctypes package,http://starship.python.net/crew/theller/ctypes/
没有评论:
发表评论