本文整理匯總了Python中xdrlib.Unpacker類的典型用法代碼示例。如果您正苦於以下問題:Python Unpacker類的具體用法?Python Unpacker怎麽用?Python Unpacker使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Unpacker類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: decode
def decode(cls, data):
'''Deserialize the data and return an object.'''
with _convert_exceptions():
xdr = Unpacker(data)
ret = cls.decode_xdr(xdr)
xdr.done()
return ret
示例2: _parse_raw_sflow_counter_sample
def _parse_raw_sflow_counter_sample(self, unpacker: Unpacker):
self.sequence_number = unpacker.unpack_uint()
self.source_id = unpacker.unpack_uint()
counters_in_sample = unpacker.unpack_uint()
for _ in range(counters_in_sample):
self.counters.append(SFlowCounterRecord(unpacker))
示例3: __init__
class Trajectory:
def __init__(self, topology, trajFileName):
# since we need to be able to do seeks, can't use osOpen
# which might return an unseekable stream
self.topology = topology
from OpenSave import osUncompressedPath
path = osUncompressedPath(trajFileName)
import os
self.trajFileSize = os.stat(path).st_size
self.traj = open(path, "rb")
from xdrlib import Unpacker
self.fileString = FileString(self.traj, 0, self.trajFileSize)
self.xdr = Unpacker(self.fileString)
self.crdStarts = []
while True:
replyobj.status("Reading frame %d header\n" % (
len(self.crdStarts) + 1))
try:
crdStart, endFrame = self._readHeader()
except ValueError, e:
raise ValueError("Frame %d: %s" %
(len(self.crdStarts) + 1, str(e)))
if endFrame > self.trajFileSize:
if not self.crdStarts:
raise ValueError("Computed size of"
" first frame (%d) greater than"
" trajectory file size (%s)" %
(endFrame, self.trajFileSize))
replyobj.warning("Truncated trajectory file;"
" skipping last partial frame.\n")
else:
self.crdStarts.append(crdStart)
if endFrame == self.trajFileSize:
break
self.xdr.set_position(endFrame)
示例4: is_tag_full
def is_tag_full(wrapped_message):
unpacker = Unpacker(wrapped_message)
try:
unpacker.unpack_uint()
unpacker.unpack_string()
except EOFError:
return False
return True
示例5: get_tlv
def get_tlv(wrapped_message):
unpacker = Unpacker(wrapped_message)
tag = unpacker.unpack_uint()
message = unpacker.unpack_string()
pos = unpacker.get_position()
buff = unpacker.get_buffer()
rest = buff[pos:]
return tag, message, rest
示例6: gmetric_read
def gmetric_read(msg):
unpacker = Unpacker(msg)
values = dict()
unpacker.unpack_int()
values['TYPE'] = unpacker.unpack_string()
values['NAME'] = unpacker.unpack_string()
values['VAL'] = unpacker.unpack_string()
values['UNITS'] = unpacker.unpack_string()
values['SLOPE'] = slope_int2str[unpacker.unpack_int()]
values['TMAX'] = unpacker.unpack_uint()
values['DMAX'] = unpacker.unpack_uint()
unpacker.done()
return values
示例7: _length_from_bytes
def _length_from_bytes(four_bytes):
"""
The RPC standard calls for the length of a message to be sent as the least
significant 31 bits of an XDR encoded unsigned integer. The most
significant bit encodes a True/False bit which indicates that this message
will be the last.
"""
from xdrlib import Unpacker
unpacker = Unpacker(four_bytes)
val = unpacker.unpack_uint()
unpacker.done()
if val < 2**31:
return (val, False)
return (val-2**31, True)
示例8: parse
def parse(self, raw_data):
packet = SFlowPacket()
data = Unpacker(raw_data)
# sFlow version (2|4|5)
packet.version = data.unpack_uint()
if packet.version != 5:
logging.error("Only support version 5.")
raise RuntimeError("Only support version 5.")
logging.debug("Get version {0}".format(packet.version))
# IP version of the Agent/Switch (1=v4|2=v6)
packet.agent_ip_version = data.unpack_uint()
if packet.agent_ip_version != 1:
logging.error("Only support IPv4.")
raise RuntimeError("Only support IPv4.")
# Agent IP address (v4=4byte|v6=16byte)
packet.agent_ip_address = ntohl(data.unpack_uint())
# sub agent id
packet.sub_agent_id = data.unpack_uint()
# datagram sequence number
packet.datagram_sequence_num = data.unpack_uint()
# switch uptime in ms
packet.switch_uptime = data.unpack_uint()
# how many samples in datagram
packet.sample_amount = data.unpack_uint()
self._parse_samples(packet, data)
return packet
示例9: read
def read(self):
fext = os.path.splitext(self.file)[-1]
assert fext == ".trr"
fp = open(self.file, "rb")
self.data = data = fp.read()
self.coords = []
self.v = {}
self.f = {}
self.up = Unpacker(data)
curpos = self.up.get_position()
datasize = len(data)
nframe = 0
#each frame begins with a header
while curpos < datasize:
#print "current position:", curpos
h = self.readHeader(nframe)
self.headers.append(h)
self.readData(nframe)
nframe = nframe + 1
curpos = self.up.get_position()
#print "end of readTraj, cur position : %d, datazize: %d" %(self.up.get_position(), datasize)
self.nframes = nframe
if self.nframes:
return 1
else:
return 0
示例10: __init__
def __init__(self, unpacker: Unpacker):
self.sequence_number = None
self.source_id = None
self.counters = []
sample_data = unpacker.unpack_opaque()
unpacker_sample_data = Unpacker(sample_data)
self._parse_raw_sflow_counter_sample(unpacker_sample_data)
示例11: _parse_raw_sflow_datagram
def _parse_raw_sflow_datagram(self, unpacker: Unpacker):
self.sflow_version = unpacker.unpack_int()
if not self.sflow_version == 5:
logging.debug("Unimplemented sFlow version: {}".format(self.sflow_version))
# TODO: read remainder if needed
return
self.agent_ip_version = unpacker.unpack_int()
if self.agent_ip_version == 1:
self.agent_ip_address = ntohl(unpacker.unpack_uint())
# TODO: implement other versions
else:
logging.debug("Unimplemented agent IP version: {}".format(self.agent_ip_version))
return
self.sub_agent_id = unpacker.unpack_uint()
self.sequence_number = unpacker.unpack_uint()
self.switch_uptime = unpacker.unpack_uint()
samples_in_datagram = unpacker.unpack_uint()
for _ in range(samples_in_datagram):
try:
self.samples.append(SFlowSample(unpacker))
except Exception as e:
logging.warning("Bad sample")
raise e
示例12: datagramReceived
def datagramReceived(self, datagram, address):
values = dict()
unpacker = Unpacker(datagram)
packet_type = unpacker.unpack_uint()
if packet_type == 128:
self.unpack_meta(unpacker)
return
elif packet_type == 136:
#unpack_metareq function works, but serves no purpose right now
#commented out unless anyone comes up with a good reason to respond
#to metadata requests.
#self.unpack_metareq(unpacker)
return
elif 128 < packet_type < 136:
self.unpack_data(unpacker, packet_type, address)
return
else:
return
示例13: __init__
def __init__(self, unpacker: Unpacker):
self.flow_format = None
self.flow = None
self.flow_format = unpacker.unpack_uint()
flow_data = unpacker.unpack_opaque()
unpacker_flow_data = Unpacker(flow_data)
if self.flow_format == SFlowFlowRecord.FLOW_DATA_RAW_HEADER:
self.flow = FlowDataRawHeader(unpacker_flow_data)
elif self.flow_format == SFlowFlowRecord.FLOW_DATA_ETHERNET_HEADER:
self.flow = FlowDataEthernetHeader(unpacker_flow_data)
elif self.flow_format == SFlowFlowRecord.FLOW_DATA_IPV4_HEADER:
self.flow = FlowDataIPv4Header(unpacker_flow_data)
elif self.flow_format == SFlowFlowRecord.FLOW_DATA_EXT_SWITCH:
self.flow = FlowDataExtSwitch(unpacker_flow_data)
else:
logging.debug('read_flow_record:Unimplemented data_format (%d)' % self.flow_format)
示例14: read_flow_record
def read_flow_record(up, sample):
"""Reads a 'struct flow_record' (p. 29)"""
flow_format = up.unpack_uint()
flow_data = up.unpack_opaque()
up_flow_data = Unpacker(flow_data)
if flow_format == FLOW_DATA_RAW_HEADER:
res = FlowRecord(sample, read_sampled_header(up_flow_data))
elif flow_format == FLOW_DATA_ETHERNET_HEADER:
res = FlowRecord(sample, read_sampled_ethernet(up_flow_data))
elif flow_format == FLOW_DATA_IPV4_HEADER:
res = FlowRecord(sample, read_sampled_ipv4(up_flow_data))
else:
res = 'read_flow_record:Unknown data_format (%d)' % flow_format
up_flow_data.done()
return res
示例15: handle
def handle(self):
data = self.request[0]
unpacker = Unpacker(data)
type = unpacker.unpack_int()
if type not in GANGLIA_DECODE: return
host = unpacker.unpack_string()
name = unpacker.unpack_string()
unpacker.unpack_int() # spoof boolean
unpacker.unpack_string() # format string
value = GANGLIA_DECODE[type](unpacker)
unpacker.done()
graphite.record_stat(name, value)