1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| XML = '''\
<pdml version="0" creator="wireshark/1.2.7">
<packet>
<proto name="geninfo" pos="0" showname="General information" size="68">
<field name="timestamp" pos="0" show="Jun 29, 2010 12:00:00.221254000" showname="Captured Time" value="1277805600.221254000" size="68"/>
</proto>
<proto name="frame" showname="Frame 1 (68 bytes on wire, 68 bytes captured)" size="68" pos="0">
<field name="frame.coloring_rule.string" showname="Coloring Rule String: udp" size="0" pos="0" show="udp"/>
</proto>
<proto name="raw" showname="Raw packet data" size="0" pos="0">
<field name="" show="No link information available" size="0" pos="0" value=""/>
</proto>
<proto name="ip" showname="Internet Protocol, Src: xx.xxx.xx.x.x (xxx.xxx.xxx.xxx), Dst: xxx.xx.xx.xx (xxx.xx.1.xxx)" size="20" pos="0">
<field name="ip.host" showname="Source or Destination Host: xx.xxx.xxx" hide="yes" size="4" pos="16" show="xxx.xx.xx.xx" value="8xx150"/>
</proto>
<proto name="udp" showname="User Datagram Protocol, Src Port: 41637 (41637), Dst Port: domain (53)" size="8" pos="20">
<field name="udp.srcport" showname="Source port: xxxx (xxxxx)" size="2" pos="20" show="xxxx" value="xxxx"/>
<field name="udp.length" showname="Length: 48" size="2" pos="24" show="48" value="0030"/>
</proto>
<proto name="dns" showname="Domain Name System (query)" size="40" pos="28">
<field name="dns.flags" showname="Flags: 0x0000 (Standard query)" size="2" pos="30" show="0x0000" value="0000"/>
<field name="dns.flags.response" showname="0... .... .... .... = Response: Message is a query" size="2" pos="30" show="0" value="0" unmaskedvalue="0000"/>
</proto>
</packet>
</pdml>
'''
import StringIO
from xml.sax import make_parser, handler
class Packet(handler.ContentHandler):
def __init__(self):
handler.ContentHandler.__init__(self)
self._dns = 0
self._tcp = 0
def startElement(self, name, attrs):
v_name = 'v_%s' % name
if hasattr(self, v_name):
getattr(self, v_name)(name, attrs)
def v_proto(self, name, attrs):
type_ = attrs['name']
if type_ == 'dns':
self._dns += 1
elif type_ == 'tcp':
self._tcp += 1
else: pass
def endDocument(self):
print 'dns: ', self._dns
print 'tcp: ', self._tcp
f = StringIO.StringIO(XML)
parser = make_parser()
parser.setContentHandler(Packet())
parser.parse(f) |
Partager