def __init__(self, address, packet): UsagePacket.__init__(self, address, packet) self.data = {} # Old version of the C usage code would call htons() twice on # the component code and packet version fields, but not the other # binary fields in the packet, so we force it to be network byte order # for the rest of the parsing self.endian_prefix = "!" header_data = self.unpack("16Bl") send_time_data = header_data[16] # The C Usage Packet sender sent corrupt IP addresses in the packets # by sending integers as octets. We'll ignore this address and use # the one in the udp header ip_data = header_data[0:16] #self.ip_address = UsagePacket.parse_address(ip_data) self.send_time = tuple((list(time.gmtime(send_time_data)))[0:6]) self.send_time_ticks = send_time_data body_str = self.packet_body[self.packet_body_offset:] while 1: match = CUsagePacket._parse_re.match(body_str) if match != None: var = match.group(1) if match.group(4) != None: val = match.group(4) else: val = match.group(2) self.data[var] = val body_str = body_str[match.end():] else: break
def __init__(self, address, packet): """ Initialize a usagepacket from a binary string containing the UDP packet contents. Arguments: self -- The new usagepacket.usagepacket object packet -- The binary packet contents Returns: New IPTimeMonitorPacket """ UsagePacket.__init__(self, address, packet) packdata = self.unpack("qB") time_sent_millis = packdata[0] time_sent = time_sent_millis / 1000 frac_secs = time_sent_millis - int(time_sent_millis) send_time = list(time.gmtime(time_sent))[0:6] send_time[5] += frac_secs self.send_time = tuple(send_time) address_form = packdata[1] if address_form == 4: address_data = self.unpack("4B") self.ip_address = socket.inet_ntop( socket.AF_INET, struct.pack('4B', *address_data)) elif address_form == 6: address_data = self.unpack("16B") self.ip_address = socket.inet_ntop( socket.AF_INET6, struct.pack('16B', *address_data))
def __str__(self): string = UsagePacket.__str__(self) for keystr in self.data.keys(): string += keystr + ": " + self.data[keystr] + "\n" return string