packet lib: make Packet class iterator

Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
FUJITA Tomonori 2012-10-05 19:01:52 +09:00
parent 9c30012955
commit 5fd538ee7f

View File

@ -22,6 +22,7 @@ class Packet(object):
super(Packet, self).__init__()
self.data = data
self.protocols = []
self.protocol_idx = 0
self.parsed_bytes = 0
if self.data:
# Do we need to handle non ethernet?
@ -55,3 +56,16 @@ class Packet(object):
for p in self.protocols:
if p.__class__.__name__ == name:
return p
def next(self):
try:
p = self.protocols[self.protocol_idx]
except:
self.protocol_idx = 0
raise StopIteration
self.protocol_idx += 1
return p
def __iter__(self):
return self