因為最近工作中,需要設計根據wirshark截獲的報文自動產生測試case的小工具,我重溫了python。這個筆記,就是根據這個工具的實現,來記錄python文法的學習過程。
這個工具的功能如所示,其中wirshark cap可以通過wirshark自身的功能匯出為pmsl格式,也就是一種XML,同時公司使用的case script也是一種XML,只是添加了一些迴圈,判斷等功能。
所以這個工具涉及檔案操作,XML解析和一些python的基本文法功能
一、PYTHON 基本文法概念
1.什麼是main函數
if __name__ == "__main__":
上面這個就是python的main函數的聲明
2. 變數申明
python對變數申明不像那麼嚴格,但也不像shell那麼隨便。一般變數通過首次賦值產生,當超出作用範圍時自動消亡。這幾句話的意思是,變數如果要使用就一定要先給他賦值。比如
print str1
一定會報錯
Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> print str1NameError: name 'str1' is not defined
所以一定要先賦值
str1 = 'hello'print str1
python一樣有全域變數和局部變數的概念。
3. python 常用的變數類型
數值型
value = 12
string
str1 = 'hello'
str1 = "hello"
這裡有個雙引號還是單引號的問題,我的理解是除非設計逸出字元,兩者都是一樣的。
a = 'let\'s us'a = "let's us"
list
也就是C語言中的數組
a = ['spam', 'eggs', 100, 1234]
支援下標訪問
a[0]a[1]
Dictionary
大家稱呼這個味字典型,其實可以類比為C語言中的結構體。因為python裡面是沒有struct這類東西的,當然python可以用class的方法替代struct。但Dictionary這個類型也可以在某些時候當做struct這個概念來用
d = {"server":"mpilgrim", "database":"master"}
這個類型是可以作為不同類型資料的container的,但要注意一個問題dictionary在插入的時候是亂序的,也就是說當我做
d["newdata"]='fda'
這個操作後,這個新加入的項會隨機加在d中,不一定在最後。
4.Python的階層
python依靠代碼縮排和:來確定代碼的前後層次關係
如下面這個函數,縮排起到了C語言中{}的作用。
def GetEnumName(attribename,valuestr): """ @author: limim @param attribename:attribname @type attribename:str @param valuestr:mmt log value @type valuestr:str @return:the enum name @rtype:str """ tree = ReadFromXml('PegasusEnumMapping.xml') root = tree.getroot() value = str(int(valuestr,16)) errorret = '' for listnode in root.iter('attrib'): strlist = listnode.attrib['name'].split('.') attriblist = attribename.split('.') substrlist = strlist[(len(strlist)-4):] subattriblist = attriblist[(len(attriblist)-4):] if substrlist == subattriblist: for enumlistnode in listnode: if enumlistnode[0].text == value: return enumlistnode[1].text else: errorret = enumlistnode[1].text print "error" print attribename print 'mmt log value' + ' ' + valuestr print 'set default value' + ' ' + errorret print '##############' return errorret
二、elementTree
這個工具處理的主要檔案格式是XML,就用elementTree這個庫來處理,下面是要處理檔案格式的一部分。
<?xml version="1.0"?><pdml version="0" creator="wireshark/1.20.0.1"><packet> <proto name="geninfo" pos="0" showname="General information" size="98"> <field name="num" pos="0" show="1246" showname="Number" value="4de" size="98"/> <field name="len" pos="0" show="98" showname="Frame Length" value="62" size="98"/> <field name="caplen" pos="0" show="98" showname="Captured Length" value="62" size="98"/> <field name="timestamp" pos="0" show="Mar 6, 2013 18:28:28.729395000 China Standard Time" showname="Captured Time" value="1362565708.729395000" size="98"/> </proto> <proto name="frame" showname="Frame 1246: 98 bytes on wire (784 bits), 98 bytes captured (784 bits)" size="98" pos="0"> <field name="frame.time" showname="Arrival Time: Mar 6, 2013 18:28:28.729395000 China Standard Time" size="0" pos="0" show="Mar 6, 2013 18:28:28.729395000"/> <field name="frame.time_epoch" showname="Epoch Time: 1362565708.729395000 seconds" size="0" pos="0" show="1362565708.729395000"/> <field name="frame.time_delta" showname="Time delta from previous captured frame: 0.000475000 seconds" size="0" pos="0" show="0.000475000"/> <field name="frame.time_delta_displayed" showname="Time delta from previous displayed frame: 0.000000000 seconds" size="0" pos="0" show="0.000000000"/> <field name="frame.time_relative" showname="Time since reference or first frame: 93.072253000 seconds" size="0" pos="0" show="93.072253000"/> <field name="frame.number" showname="Frame Number: 1246" size="0" pos="0" show="1246"/> <field name="frame.len" showname="Frame Length: 98 bytes (784 bits)" size="0" pos="0" show="98"/> <field name="frame.cap_len" showname="Capture Length: 98 bytes (784 bits)" size="0" pos="0" show="98"/> <field name="frame.marked" showname="Frame is marked: False" size="0" pos="0" show="0"/> <field name="frame.ignored" showname="Frame is ignored: False" size="0" pos="0" show="0"/> <field name="frame.protocols" showname="Protocols in frame: eth:ip:udp:mmtss:sicap" size="0" pos="0" show="eth:ip:udp:mmtss:sicap"/> <field name="frame.coloring_rule.name" showname="Coloring Rule Name: SICAP" size="0" pos="0" show="SICAP"/> <field name="frame.coloring_rule.string" showname="Coloring Rule String: sicap" size="0" pos="0" show="sicap"/> </proto> <proto name="eth" showname="Ethernet II, Src: 192.168.254.1 (00:0f:bb:69:93:ee), Dst: DCT-INF (00:10:18:cb:b5:fd)" size="14" pos="0"> <field name="eth.dst" showname="Destination: DCT-INF (00:10:18:cb:b5:fd)" size="6" pos="0" show="00:10:18:cb:b5:fd" value="001018cbb5fd"> <field name="eth.addr" showname="Address: DCT-INF (00:10:18:cb:b5:fd)" size="6" pos="0" show="00:10:18:cb:b5:fd" value="001018cbb5fd"/> <field name="eth.ig" showname=".... ...0 .... .... .... .... = IG bit: Individual address (unicast)" size="3" pos="0" show="0" value="0" unmaskedvalue="001018"/> <field name="eth.lg" showname=".... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)" size="3" pos="0" show="0" value="0" unmaskedvalue="001018"/> </field> <field name="eth.src" showname="Source: 192.168.254.1 (00:0f:bb:69:93:ee)" size="6" pos="6" show="00:0f:bb:69:93:ee" value="000fbb6993ee"> <field name="eth.addr" showname="Address: 192.168.254.1 (00:0f:bb:69:93:ee)" size="6" pos="6" show="00:0f:bb:69:93:ee" value="000fbb6993ee"/> <field name="eth.ig" showname=".... ...0 .... .... .... .... = IG bit: Individual address (unicast)" size="3" pos="6" show="0" value="0" unmaskedvalue="000fbb"/> <field name="eth.lg" showname=".... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)" size="3" pos="6" show="0" value="0" unmaskedvalue="000fbb"/> </field> <field name="eth.type" showname="Type: IP (0x0800)" size="2" pos="12" show="0x0800" value="0800"/> </proto>
1. 檔案讀取
from xml.etree.ElementTree import ElementTree, Elementimport sysdef ReadFromXml(path): ''''read from xml and prase it author:limin path: the file path return ElementTree''' tree = ElementTree() tree.parse(path); return tree
通過
tree = ElementTree()tree.parse(path);
就獲得了XML檔案的指標
在偵錯模式下,可以看到tree就是根據讀入的XML檔案用不同層級的NODE構成的結構體。這個時候操作就很方便了,我們可以先拿到root,在遍曆整個tree就可以方便的讀取XML中任意一個節點的attrib,text,tag這些內容。
def ReadPacketContent(tree,framenum): ''' read the packet content node:the xml node num: the index of the node ''' SicCapMessage = [] root = tree.getroot() #check the boundary num = 0 flag = 0 for node in root: # proto level node if flag == 1: break num = num + 1 #print protonode.tag,protonode.attrib for protonode in node: if protonode.attrib['name'] == 'frame': frameList = parseFrameHead(protonode) if frameList[1] != str(framenum): break else: SicCapMessage.insert(0,frameList) print "find the frame, number is" + frameList[1] flag = 1 print num print len(root) if num == 0 or num > len(root) - 1: print "can not find the frame num" return SicCapMessage for protonode in root[num - 1]: if protonode.attrib['name'] == 'sicap': #dictPrint(parseSiCapMessage(protonode)) SicCapMessage.extend(parseSiCapMessage(protonode)) return SicCapMessage
可以看到
protonode.attrib
就是一個dictionary類型的變數
2.檔案寫入
讀取的問題解決了,剩下的就是寫入了。只用調用
tree.write('test.xml','UTF-8')
就可以將記憶體中的tree寫入檔案