如何利用python操作注冊表
注冊表是windows管理配置系統(tǒng)運行參數(shù)的一個核心數(shù)據(jù)庫。在這個數(shù)據(jù)庫里整合集成了全部系統(tǒng)和應(yīng)用程序的初始化信息;其中包含了硬件設(shè)備的說明、相互關(guān)聯(lián)的應(yīng)用程序與文檔文件、窗口顯示方式、網(wǎng)絡(luò)連接參數(shù)、甚至有關(guān)系到計算機安全的網(wǎng)絡(luò)共享設(shè)置 。
1.讀取
讀取用的方法是OpenKey方法:打開特定的key
winreg.OpenKey(key,sub_key,res=0,sam=KEY_READ)
例子:此例子是顯示了本機網(wǎng)絡(luò)配置的一些注冊表項
import winregkey = winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,r'SYSTEMCurrentControlSetServicesTcpipParametersInterfaces{0E184877-D910-4877-B 4C2-04F487B6DBB7}')#獲取該鍵的所有鍵值,遍歷枚舉try: i=0 while 1: #EnumValue方法用來枚舉鍵值,EnumKey用來枚舉子鍵 name,value,type = _winreg.EnumValue(key,i) print repr(name),value,type i+=1except WindowsError: print #假如知道鍵名,也可以直接取值value,type = _winreg.QueryValueEx(key,'DhcpDefaultGateway')print '默認網(wǎng)關(guān)地址----',value,type
2.創(chuàng)建 修改注冊表
創(chuàng)建key:_winreg.CreateKey(key,sub_key)
刪除key: _winreg.DeleteKey(key,sub_key)
刪除鍵值:_winreg.DeleteValue(key,value)
給新建的key賦值:_winreg.SetValue(key,sub_key,type,value)
例子:
#!/usr/bin/env python#coding=utf-8import winreg key=winreg.OpenKey(_winreg.HKEY_CURRENT_USER,r'SoftwareMicrosoftWindowsCurrentVersionExplorer')#刪除鍵_winreg.DeleteKey(key, 'Advanced')#刪除鍵值_winreg.DeleteValue(key, 'IconUnderline')#創(chuàng)建新的newKey = _winreg.CreateKey(key,'MyNewkey') #給新創(chuàng)建的鍵添加鍵值_winreg.SetValue(newKey,'ValueName',0,'ValueContent')
3. 權(quán)限問題
寫完的Python腳本必須用管理員權(quán)限運行,才能對注冊表進行寫操作。否則會報PermissionError異常這個時候需要調(diào)用Windows的API,重新啟動一遍程序 runas administrator,將原來的程序退出。
代碼也很簡單
from __future__ import print_functionimport ctypes, sysdef is_admin(): try: return ctypes.windll.shell32.IsUserAnAdmin() except: return Falseif is_admin(): # 將要運行的代碼加到這里else: if sys.version_info[0] == 3: ctypes.windll.shell32.ShellExecuteW(None, 'runas', sys.executable, __file__, None, 1) else:#in python2.x ctypes.windll.shell32.ShellExecuteW(None, u'runas', unicode(sys.executable), unicode(__file__), None, 1)
網(wǎng)上搜的都是python2的, 自己寫代碼的時候總是搞不正確的包,然后用
pip search winreg
結(jié)果是:
winreg-helpers (0.1.1) - Helper functions for reading/writing to the Windows Registry.
裝這個包就可以了。
解決問題,每次在鏈接vpn后,發(fā)現(xiàn)browser就打不開了,需要手動去將其去掉。現(xiàn)在只要執(zhí)行一下以下這個腳本,就解決了問題:
import winregINTERNET_SETTINGS = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r’SoftwareMicrosoftWindowsCurrentVersionInternet Settings’, 0, winreg.KEY_ALL_ACCESS)def set_key(name, value): _, reg_type = winreg.QueryValueEx(INTERNET_SETTINGS, name) winreg.SetValueEx(INTERNET_SETTINGS, name, 0, reg_type, value)set_key(’ProxyEnable’, 0)#set_key(’ProxyOverride’, u’*.local;<local>’) # Bypass the proxy for localhost#set_key(’ProxyServer’, u’X.X.X.X:8080’)
測試一下,通過。
以上就是如何利用python操作注冊表的詳細內(nèi)容,更多關(guān)于python 注冊表的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. PHP橋接模式Bridge Pattern的優(yōu)點與實現(xiàn)過程2. asp.net core項目授權(quán)流程詳解3. html中的form不提交(排除)某些input 原創(chuàng)4. js select支持手動輸入功能實現(xiàn)代碼5. CSS3中Transition屬性詳解以及示例分享6. bootstrap select2 動態(tài)從后臺Ajax動態(tài)獲取數(shù)據(jù)的代碼7. vue使用moment如何將時間戳轉(zhuǎn)為標準日期時間格式8. 開發(fā)效率翻倍的Web API使用技巧9. jsp文件下載功能實現(xiàn)代碼10. ASP常用日期格式化函數(shù) FormatDate()
