一樣的套路又來了,繼續嘗試從配置文件中讀取敏感的信息,此次輪到的是MySQL-Front。html
MySQL-Front就一款開源的mysql管理工具,官方網站http://www.mysqlfront.de/ 。python
MySQL-Front的配置文件存在用戶目錄下,環境變量是%appdata%
。
在windows7下的存儲路徑是:
C:\Users\%user%\AppData\Roaming\MySQL-Front\Accounts.xml
Accounts.xml這個XML文件裏面存儲了全部重要的信息,且密碼默認不是加密的。上次我忘記mysql root用戶的密碼,打開這個文件立馬就找回密碼,
這個算是不加密的好處吧。→_→
不過總有刁民想害朕,仍是須要保護好這些重要的信息,以避免被壞人讀取到。mysql
Accounts.xml 中的內容是被壓縮成一行的。須要格式化成好看的格式。這類在線工具搜索一下就能夠找獲得。jquery
Accounts.xml 格式化後的內容以下:web
<?xml version="1.0" encoding="utf-8"?>
<accounts version="1.1.0">
<default>127.0.0.1</default>
<account name="127.0.0.1">
<lastlogin>42847.9391816088</lastlogin>
<manualurl version="5.0.22-community-nt"></manualurl>
<connection>
<database></database>
<host>127.0.0.1</host>
<library>
<filename>libMySQL.dll</filename>
<tunnel_url></tunnel_url>
</library>
<password encode="none">root</password>
<port>3306</port>
<user>root</user>
</connection>
<favorites />
</account>
<account name="daqin">
<lastlogin>0</lastlogin>
<manualurl version=""></manualurl>
<connection>
<database></database>
<host>127.0.0.1</host>
<library>
<filename>libMySQL.dll</filename>
<tunnel_url></tunnel_url>
</library>
<password encode="none">daqin</password>
<port>3306</port>
<user>daqin</user>
</connection>
<favorites />
</account>
</accounts>
我出於要練習的目的,想要用python的XML標準庫處理XML ,可是發python 內置提供了好幾種方法:xml.sax xml.dom xml.minidom
以及還有xml.parsers.expat ,選擇太多,仍是決定用PyQuery,PyQuery是依賴於lxml實現的jquery風格的xml解析和處理庫。
lxml算是python很重要的庫了,已知pandas,BeautifulSoup等等這些庫有部分功能依賴lxml。sql
輸入命令安裝便可:
pip install pyquery
windows
看完教程後就能把代碼寫出來了↓↓↓markdown
# -*- coding: utf-8 -*-
""" Created on 2017-04-22 22:53:35 @author: codegay """
import os
from pyquery import PyQuery as pyq
xmlpath = os.environ['appdata']+r'\MySQL-Front\Accounts.xml'
root = pyq(filename=xmlpath)
for r in root('connection').items():
print("----------------------------------------------")
print('host:',r('host').text())
print('username:',r('user').text())
print('password:',r('password').text())
運行代碼後輸出:app
---------------------------------------------- host: 127.0.0.1 username: root password: root ---------------------------------------------- host: 127.0.0.1 username: daqin password: daqin
codegay 2017年5月1日14:14:09dom
ps:第一次嘗試用小書匠編輯器客戶端,支持markdown,支持一鍵發佈到cnblogs,推薦一下。
pyquery 官方文檔(英文) http://pyquery.rtfd.org/
Python爬蟲利器六之PyQuery的用法 http://cuiqingcai.com/2636.html
(在線工具)XML格式化 http://web.chacuo.net/formatxml
小書匠編輯器 http://soft.xiaoshujiang.com/download.html