#coding=GB2312 import os import sys import subprocess import _winreg LOCAL_CONNECT_NAME = '本地连接' WIFI_CONNECT_NAME = '无线网络连接' INTERNET_SETTING_REG = r'Software\Microsoft\Windows\CurrentVersion\Internet Settings' PROXY_ENABLE_NAME = 'ProxyEnable' def setEnable(connName, enable=True): str_enable = '' if enable: str_enable = 'enable' else: str_enable = 'disable' cmd='netsh int set int name=%s admin=%s' % ('"'+connName+'"', str_enable) print cmd ret =subprocess.call(cmd, shell=True) #print 'ret=',ret if ret != 0: print '网络连接设置失败' else: print '网络连接设置成功' def setIEProxyEnable(val= 1): try: #OpenKey默认以只读的权限打开,由于没有设置_winreg.KEY_ALL_ACCESS #导致后面的setValueEx挂掉,出现WindowsError [5] #要特别注意打开权限 key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,INTERNET_SETTING_REG,0, _winreg.KEY_ALL_ACCESS) v, t = _winreg.QueryValueEx(key, PROXY_ENABLE_NAME) if v != val: _winreg.SetValueEx(key, PROXY_ENABLE_NAME, 0, t, val) except Exception,e: print '设置IE代理失败:' , e.message sys.exit(0) print '设置代理成功' if __name__ == '__main__': reload(sys) sys.setdefaultencoding('GB2312') setIEProxyEnable(0) if len(sys.argv) != 2: print 'switch_net.py N\r\n0:local 1:wifi.' sys.exit(0) if int(sys.argv[1]) == 0: setEnable(LOCAL_CONNECT_NAME) setEnable(WIFI_CONNECT_NAME,False) setIEProxyEnable() else: setEnable(LOCAL_CONNECT_NAME,False) setEnable(WIFI_CONNECT_NAME) setIEProxyEnable(0)
原文地址:http://blog.csdn.net/davidsu33/article/details/46521309