def to_xdr_bytes(self) -> bytes: packer = Packer() self.pack(packer) return packer.get_buffer()
def _pack(avps): p = Packer() for a in avps: a.encode(p) return p.get_buffer()
def _gmetric(self, name, val, metric_type, units, slope, tmax, dmax, group, title, description, spoof): meta = Packer() HOSTNAME = socket.gethostname() if spoof: SPOOF_ENABLED = 1 else: SPOOF_ENABLED = 0 # Meta data about a metric packet_type = 128 meta.pack_int(packet_type) if SPOOF_ENABLED == 1: meta.pack_string(spoof) else: meta.pack_string(HOSTNAME) meta.pack_string(name) meta.pack_int(SPOOF_ENABLED) meta.pack_string(metric_type) meta.pack_string(name) meta.pack_string(units) meta.pack_int(METRIC_SLOPES[slope]) # map slope string to int meta.pack_uint(int(tmax)) meta.pack_uint(int(dmax)) extra_data = 0 if group: extra_data += 1 if title: extra_data += 1 if description: extra_data += 1 meta.pack_int(extra_data) if group: for g in group.split(','): meta.pack_string("GROUP") meta.pack_string(g) if title: meta.pack_string("TITLE") meta.pack_string(title) if description: meta.pack_string("DESC") meta.pack_string(description) # Actual data sent in a separate packet data = Packer() packet_type = METRIC_TYPES[metric_type] data.pack_int(packet_type) if SPOOF_ENABLED == 1: data.pack_string(spoof) else: data.pack_string(HOSTNAME) data.pack_string(name) data.pack_int(SPOOF_ENABLED) if metric_type in ['int8', 'uint8', 'int16', 'uint16', 'int32']: data.pack_string("%d") data.pack_int(int(val)) if metric_type == 'uint32': data.pack_string("%u") data.pack_uint(long(val)) if metric_type == 'string': data.pack_string("%s") data.pack_string(str(val)) if metric_type == 'float': data.pack_string("%f") data.pack_float(float(val)) if metric_type == 'double': data.pack_string("%f") data.pack_double(float(val)) # XXX - double or float? return meta.get_buffer(), data.get_buffer()
def encode(self): '''Return the serialized bytes for the object.''' with _convert_exceptions(): xdr = Packer() self.encode_xdr(xdr) return xdr.get_buffer()
import socket from xdrlib import Packer host = 'localhost' port = 8000 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((host, port)) p = Packer() p.pack_int(1) s.send(p.get_buffer()) data = s.recv(1024) s.close() print 'recibido', repr(data)