python切換網路連接

來源:互聯網
上載者:User

標籤:python   網路連接   

#coding=GB2312import osimport sys import subprocessimport _winregLOCAL_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 cmdret =subprocess.call(cmd, shell=True)#print 'ret=',retif 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.messagesys.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)

python切換網路連接

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.