Python struct.Struct() Examples
The following are 30
code examples of struct.Struct().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
struct
, or try the search function
.

Example #1
Source File: hp_imc_7_3_10002_download_backups.py From poc with BSD 3-Clause "New" or "Revised" License | 7 votes |
def send_dbman_msg(self, opcode, msg): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.target_ip, self.dbman_port)) encodedMsg = encoder.encode(msg, defMode=True) msgLen = len(encodedMsg) values = (opcode, msgLen, encodedMsg) s = struct.Struct(">ii%ds" % msgLen) packed_data = s.pack(*values) sock.send(packed_data) res = sock.recv(1024) if res is not None: print "Received 10002 response..." sock.close()
Example #2
Source File: text2bin.py From DOTA_models with Apache License 2.0 | 6 votes |
def go(fhs): fmt = None with open(opt_vocab, 'w') as vocab_out: with open(opt_output, 'w') as vecs_out: for lines in izip(*fhs): parts = [line.split() for line in lines] token = parts[0][0] if any(part[0] != token for part in parts[1:]): raise IOError('vector files must be aligned') print >> vocab_out, token vec = [sum(float(x) for x in xs) for xs in zip(*parts)[1:]] if not fmt: fmt = struct.Struct('%df' % len(vec)) vecs_out.write(fmt.pack(*vec))
Example #3
Source File: primitives.py From aiozk with MIT License | 6 votes |
def parse(cls, buff, offset): """ Given a buffer and offset, returns the parsed value and new offset. Parses the ``size_primitive`` first to determine how many more bytes to consume to extract the value. """ size, offset = cls.size_primitive.parse(buff, offset) if size == -1: return None, offset var_struct = struct.Struct("!%ds" % size) value = var_struct.unpack_from(buff, offset)[0] value = cls.parse_value(value) offset += var_struct.size return value, offset
Example #4
Source File: faraday_msg.py From Faraday-Software with GNU General Public License v3.0 | 6 votes |
def __init__(self): """ A state machine that handles the functionality needed to create and transmit a message """ # Constants self.MAX_PAYLOAD_LENGTH = 40 # TBD self.MAX_MSG_DATA_LENGTH = 38 # TDB # States self.STATE_IDLE = 0 self.STATE_INIT = 1 self.STATE_FRAGMENT = 2 self.STATE_TX = 3 # Frame Types self.MSG_START = 255 self.MSG_DATA = 254 self.MSG_END = 253 # Variables self.list_packets = [] # Frame Definitions self.pkt_datagram_frame = struct.Struct('1B 40s') # Fixed self.pkt_start = struct.Struct('9s 3B') # Fixed self.pkt_data = struct.Struct('2B 38s') # Variable Data Length self.pkt_end = struct.Struct('1B') # Fixed
Example #5
Source File: faraday_msg.py From Faraday-Software with GNU General Public License v3.0 | 6 votes |
def __init__(self): """ The message application object contains all the functions, definitions, and state machines needed to implement a bare-bones text message application using the Faraday command application "experimental RF Packet Forward" functionality." """ # Identification Variables self.local_device_callsign = 'kb1lqc' self.local_device_node_id = 1 # Initialize objects self.faraday_Rx = faradaybasicproxyio.proxyio() self.faraday_Rx_SM = MsgStateMachineRx() # Initialize variables # Frame Definitions (Should be combined later with TX?) self.pkt_datagram_frame = struct.Struct('1B 41s') # Fixed self.pkt_start = struct.Struct('9s 3B') # Fixed self.pkt_data = struct.Struct('2B 39s') # Variable Data Length self.pkt_end = struct.Struct('1B') # Fixed
Example #6
Source File: faraday_msg.py From Faraday-Software with GNU General Public License v3.0 | 6 votes |
def __init__(self): """ A state machine that handles the functionality needed to create and transmit a message """ # Constants self.MAX_PAYLOAD_LENGTH = 40 # TBD self.MAX_MSG_DATA_LENGTH = 38 # TDB # States self.STATE_IDLE = 0 self.STATE_INIT = 1 self.STATE_FRAGMENT = 2 self.STATE_TX = 3 # Frame Types self.MSG_START = 255 self.MSG_DATA = 254 self.MSG_END = 253 # Variables self.list_packets = [] # Frame Definitions self.pkt_datagram_frame = struct.Struct('1B 40s') # Fixed self.pkt_start = struct.Struct('9s 3B') # Fixed self.pkt_data = struct.Struct('2B 38s') # Variable Data Length self.pkt_end = struct.Struct('1B') # Fixed
Example #7
Source File: faraday_msg.py From Faraday-Software with GNU General Public License v3.0 | 6 votes |
def __init__(self): """ The message application object contains all the functions, definitions, and state machines needed to implement a bare-bones text message application using the Faraday command application "experimental RF Packet Forward" functionality." """ # Identification Variables self.local_device_callsign = 'kb1lqc' self.local_device_node_id = 1 # Initialize objects self.faraday_Rx = faradaybasicproxyio.proxyio() self.faraday_Rx_SM = MsgStateMachineRx() # Initialize variables # Frame Definitions (Should be combined later with TX?) self.pkt_datagram_frame = struct.Struct('1B 41s') # Fixed self.pkt_start = struct.Struct('9s 3B') # Fixed self.pkt_data = struct.Struct('2B 39s') # Variable Data Length self.pkt_end = struct.Struct('1B') # Fixed
Example #8
Source File: faraday_msg.py From Faraday-Software with GNU General Public License v3.0 | 6 votes |
def __init__(self): """ A state machine that handles the functionality needed to create and transmit a message """ # Constants self.MAX_PAYLOAD_LENGTH = 40 # TBD self.MAX_MSG_DATA_LENGTH = 38 # TDB # States self.STATE_IDLE = 0 self.STATE_INIT = 1 self.STATE_FRAGMENT = 2 self.STATE_TX = 3 # Frame Types self.MSG_START = 255 self.MSG_DATA = 254 self.MSG_END = 253 # Variables self.list_packets = [] # Frame Definitions self.pkt_datagram_frame = struct.Struct('1B 40s') # Fixed self.pkt_start = struct.Struct('9s 3B') # Fixed self.pkt_data = struct.Struct('2B 38s') # Variable Data Length self.pkt_end = struct.Struct('1B') # Fixed
Example #9
Source File: msg.py From Faraday-Software with GNU General Public License v3.0 | 6 votes |
def __init__(self): """ A state machine that handles the functionality needed to create and transmit a message """ #Constants self.MAX_PAYLOAD_LENGTH = 20 #TBD self.MAX_MSG_DATA_LENGTH = 18 #TDB #States self.STATE_IDLE = 0 self.STATE_INIT = 1 self.STATE_FRAGMENT = 2 self.STATE_TX = 3 #Frame Types self.MSG_START = 255 self.MSG_DATA = 254 self.MSG_END = 253 #Variables self.list_packets = [] #Frame Definitions self.pkt_datagram_frame = struct.Struct('1B 19s') self.pkt_start = struct.Struct('9s 3B') self.pkt_data = struct.Struct('2B 18s') self.pkt_end = struct.Struct('1B')
Example #10
Source File: commandmodule.py From Faraday-Software with GNU General Public License v3.0 | 6 votes |
def create_local_telem_update_packet(): """ A predefined command packet generator that provides a payload for the local telemetry update command. :Return: A completed packet .. todo:: This should be deprecated into a single "dummy" payload function """ packet_struct = struct.Struct('>B') packet = packet_struct.pack(255) return packet ############## ## Command = Send RF Data Now ##############
Example #11
Source File: commandmodule.py From Faraday-Software with GNU General Public License v3.0 | 6 votes |
def create_rf_telem_update_packet(): """ A predefined command packet generator that provides a payload for the RF telemetry update command. :Return: A completed packet .. todo:: This should be deprecated into a single "dummy" payload function """ packet_struct = struct.Struct('>B') packet = packet_struct.pack(255) return packet ############## ## Command = Update RF frequency ##############
Example #12
Source File: commandmodule.py From Faraday-Software with GNU General Public License v3.0 | 6 votes |
def create_update_rf_frequency_packet(frequency_mhz): """ This function generates the command packet that updates the Faraday CC430 radio frequency. :param frequency_mhz: The frequency in MHz as an Integer or Float. :Return: A completed packet (string of bytes) """ freq_list = create_freq_list(float(frequency_mhz)) packet_struct = struct.Struct('3B') packet = packet_struct.pack(freq_list[0], freq_list[1], freq_list[2]) return packet ############## ## Command = Change CC430 PATable Settings ## ucharPATable_Setting = Byte that is places into the PA Table ##############
Example #13
Source File: commandmodule.py From Faraday-Software with GNU General Public License v3.0 | 6 votes |
def create_update_rf_patable_packet(ucharPATable_Setting): """ This function generates the command packet that updates the Faraday CC430 RF power setting. .. note:: A setting of 152 is the maximum output power, any number higher than 152 will be sent as a value of 152. :param ucharPATable_Setting: The RF power table register setting byte for the CC430. :Return: A completed packet (string of bytes) """ packet_struct = struct.Struct('1B') packet = packet_struct.pack(ucharPATable_Setting) return packet ############## ## Command = RESET device debug Flash ## Note: Not payload packet needed, returing 1 byte of 0 just for placeholder ##############
Example #14
Source File: commandmodule.py From Faraday-Software with GNU General Public License v3.0 | 6 votes |
def CreatePacketMsgExperimental(msg_cmd, dest_callsign, dest_device_id, data_len, data): """ This function creates the command packet expected by the command application routine that forwards a supplied payload over RF to a remote Faraday device. This is a very useful yet simple and un-optimized method of sending abritray data from one unit to another. :param dest_callsign: The callsign of the target Faraday unit :param dest_device_id: The device ID number of the target Faraday unit :param data_len: The length in bytes of the data payload supplied :param data: The data payload to be sent to the remote Faraday device over RF :Return: A completed packet (string of bytes) .. note: The data payload length cannot be larger than the maximum transmissible unit. Also, data only needs to be a string of bytes so both strings and packed binary data are acceptable. :Example: Creates an experiement message packet that sends "Testing" to the remote unit. >>> CreatePacketMsgExperimental(0, "KB1LQD", 1, 7, "Testing") """ packet_struct = struct.Struct('1B9s2B42s') packet = packet_struct.pack(msg_cmd, str(dest_callsign).upper(), dest_device_id, data_len, data) return packet
Example #15
Source File: layer_2_protocol.py From Faraday-Software with GNU General Public License v3.0 | 6 votes |
def __init__(self, input_channel, serial_physical_obj): #Initialize class variables self.rx_packet_queue = Queue.Queue() self.rx_data_payload_queue = Queue.Queue() self.logic_startbyte_received = False self.logic_escapebyte_received = False self.logic_stopbyte_received = False self.receiver_class = Receiver_Datalink_Device_State_Parser_Class(input_channel, serial_physical_obj) self.enable_flag = True self.max_payload_size = 6 self.datalink_packet_format = 'c' + str(self.max_payload_size) + 'c' + 'c' self.reconstruct_data = '' self.timer_interval = 5 self.timer = threading.Timer(self.timer_interval, self.timer_trip) self.datalink_packet_struct = struct.Struct('BBB125s') #Start threading.Thread.__init__(self) self.start() #Starts the run() function and thread
Example #16
Source File: hermesmessage.py From Faraday-Software with GNU General Public License v3.0 | 6 votes |
def __init__(self): """ A state machine that handles the functionality needed to create and transmit a message """ # Constants self.MAX_PAYLOAD_LENGTH = 40 # TBD self.MAX_MSG_DATA_LENGTH = 38 # TDB # States self.STATE_IDLE = 0 self.STATE_INIT = 1 self.STATE_FRAGMENT = 2 self.STATE_TX = 3 # Frame Types self.MSG_START = 255 self.MSG_DATA = 254 self.MSG_END = 253 # Variables self.list_packets = [] # Frame Definitions self.pkt_datagram_frame = struct.Struct('1B 40s') # Fixed self.pkt_start = struct.Struct('9s 3B') # Fixed self.pkt_data = struct.Struct('2B 38s') # Variable Data Length self.pkt_end = struct.Struct('1B') # Fixed
Example #17
Source File: hermesmessage.py From Faraday-Software with GNU General Public License v3.0 | 6 votes |
def __init__(self): """ The message application object contains all the functions, definitions, and state machines needed to implement a bare-bones text message application using the Faraday command application "experimental RF Packet Forward" functionality." """ # Identification Variables self.local_device_callsign = 'kb1lqc' self.local_device_node_id = 1 # Initialize objects self.faraday_Rx = faradaybasicproxyio.proxyio() self.faraday_Rx_SM = MsgStateMachineRx() # Initialize variables # Frame Definitions (Should be combined later with TX?) self.pkt_datagram_frame = struct.Struct('1B 41s') # Fixed self.pkt_start = struct.Struct('9s 3B') # Fixed self.pkt_data = struct.Struct('2B 39s') # Variable Data Length self.pkt_end = struct.Struct('1B') # Fixed
Example #18
Source File: standard.py From aumfor with GNU General Public License v3.0 | 6 votes |
def __init__(self, base, config, layered = False, **kwargs): addrspace.BaseAddressSpace.__init__(self, base, config, **kwargs) self.as_assert(base == None or layered, 'Must be first Address Space') self.as_assert(config.LOCATION.startswith("file://"), 'Location is not of file scheme') path = urllib.url2pathname(config.LOCATION[7:]) self.as_assert(os.path.exists(path), 'Filename must be specified and exist') self.name = os.path.abspath(path) self.fname = self.name self.mode = 'rb' if config.WRITE: self.mode += '+' self.fhandle = open(self.fname, self.mode) self.fhandle.seek(0, 2) self.fsize = self.fhandle.tell() self._long_struct = struct.Struct("=I") # Abstract Classes cannot register options, and since this checks config.WRITE in __init__, we define the option here
Example #19
Source File: params_builder.py From ontology-python-sdk with GNU Lesser General Public License v3.0 | 6 votes |
def push_vm_param(self, param): if isinstance(param, bytearray) or isinstance(param, bytes): self.push_bytearray(param) elif isinstance(param, str): self.push_bytearray(bytes(param.encode())) elif isinstance(param, bool): self.push_bool(param) elif isinstance(param, int): self.push_int(param) elif isinstance(param, dict): self.push_map(param) elif isinstance(param, list): self.create_code_params_script_builder(param) self.push_int(len(param)) self.emit(PACK) elif isinstance(param, Struct): self.push_struct(param) elif isinstance(param, Address): self.push_bytearray(param.to_bytes()) elif isinstance(param, Account): self.push_bytearray(param.get_address().to_bytes()) else: raise SDKException(ErrorCode.other_error('parameter type is error'))
Example #20
Source File: base64.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def _85encode(b, chars, chars2, pad=False, foldnuls=False, foldspaces=False): # Helper function for a85encode and b85encode if not isinstance(b, bytes_types): b = memoryview(b).tobytes() padding = (-len(b)) % 4 if padding: b = b + b'\0' * padding words = struct.Struct('!%dI' % (len(b) // 4)).unpack(b) chunks = [b'z' if foldnuls and not word else b'y' if foldspaces and word == 0x20202020 else (chars2[word // 614125] + chars2[word // 85 % 7225] + chars[word % 85]) for word in words] if padding and not pad: if chunks[-1] == b'z': chunks[-1] = chars[0] * 5 chunks[-1] = chunks[-1][:-padding] return b''.join(chunks)
Example #21
Source File: defines.py From CoAPthon3 with MIT License | 6 votes |
def get_option_flags(option_num): """ Get Critical, UnSafe, NoCacheKey flags from the option number as per RFC 7252, section 5.4.6 :param option_num: option number :return: option flags :rtype: 3-tuple (critical, unsafe, no-cache) """ opt_bytes = array.array('B', '\0\0') if option_num < 256: s = struct.Struct("!B") s.pack_into(opt_bytes, 0, option_num) else: s = struct.Struct("H") s.pack_into(opt_bytes, 0, option_num) critical = (opt_bytes[0] & 0x01) > 0 unsafe = (opt_bytes[0] & 0x02) > 0 nocache = ((opt_bytes[0] & 0x1e) == 0x1c) return (critical, unsafe, nocache)
Example #22
Source File: serializer.py From CoAPthon3 with MIT License | 6 votes |
def read_option_value_from_nibble(nibble, pos, values): """ Calculates the value used in the extended option fields. :param nibble: the 4-bit option header value. :return: the value calculated from the nibble and the extended option value. """ if nibble <= 12: return nibble, pos elif nibble == 13: tmp = struct.unpack("!B", values[pos].to_bytes(1, "big"))[0] + 13 pos += 1 return tmp, pos elif nibble == 14: s = struct.Struct("!H") tmp = s.unpack_from(values[pos:].to_bytes(2, "big"))[0] + 269 pos += 2 return tmp, pos else: raise AttributeError("Unsupported option nibble " + str(nibble))
Example #23
Source File: text2bin.py From yolo_v2 with Apache License 2.0 | 6 votes |
def go(fhs): fmt = None with open(opt_vocab, 'w') as vocab_out: with open(opt_output, 'w') as vecs_out: for lines in izip(*fhs): parts = [line.split() for line in lines] token = parts[0][0] if any(part[0] != token for part in parts[1:]): raise IOError('vector files must be aligned') print >> vocab_out, token vec = [sum(float(x) for x in xs) for xs in zip(*parts)[1:]] if not fmt: fmt = struct.Struct('%df' % len(vec)) vecs_out.write(fmt.pack(*vec))
Example #24
Source File: structreader.py From me-ica with GNU Lesser General Public License v2.1 | 5 votes |
def unpack(self, fmt): ''' Unpack values from contained buffer Unpacks values from ``self.buf`` and updates ``self.ptr`` to the position after the read data. Parameters ---------- fmt : str format string as for ``unpack`` Returns ------- values : tuple values as unpacked from ``self.buf`` according to `fmt` ''' # try and get a struct corresponding to the format string from # the cache pkst = self._cache.get(fmt) if pkst is None: # struct not in cache # if we've not got a default endian, or the format has an # explicit endianness, then we make a new struct directly # from the format string if self.endian is None or fmt[0] in _ENDIAN_CODES: pkst = Struct(fmt) else: # we're going to modify the endianness with our # default. endian_fmt = self.endian + fmt pkst = Struct(endian_fmt) # add an entry in the cache for the modified format # string as well as (below) the unmodified format # string, in case we get a format string with the same # endianness as default, but specified explicitly. self._cache[endian_fmt] = pkst self._cache[fmt] = pkst values = pkst.unpack_from(self.buf, self.ptr) self.ptr += pkst.size return values
Example #25
Source File: short_id.py From zun with Apache License 2.0 | 5 votes |
def _to_byte_string(value, num_bits): """Convert an integer to a big-endian string of bytes with padding. Padding is added at the end (i.e. after the least-significant bit) if required. """ shifts = range(num_bits - 8, -8, -8) byte_at = lambda off: ( # noqa: E731 (value >> off if off >= 0 else value << -off) & 0xff) return ''.join(struct.Struct(">B").pack(byte_at(offset)) for offset in shifts)
Example #26
Source File: connections.py From ServerlessCrawler-VancouverRealState with MIT License | 5 votes |
def read_struct(self, fmt): s = struct.Struct(fmt) result = s.unpack_from(self._data, self._position) self._position += s.size return result
Example #27
Source File: dev_appserver.py From browserscope with Apache License 2.0 | 5 votes |
def _GenerateRequestLogId(): """Generates the request log id for the current request.""" sec = int(_request_time) usec = int(1000000 * (_request_time - sec)) h = hashlib.sha1(str(_request_id)).digest()[:4] packed = struct.Struct('> L L').pack(sec, usec) return binascii.b2a_hex(packed + h)
Example #28
Source File: packer.py From segpy with GNU Affero General Public License v3.0 | 5 votes |
def make_header_packer(header_format_class, endian='>'): cformat, field_name_allocations = compile_struct( header_format_class, getattr(header_format_class, 'START_OFFSET_IN_BYTES', 0), getattr(header_format_class, 'LENGTH_IN_BYTES', None), endian) structure = Struct(cformat) one_to_one = all(len(fields) == 1 for fields in field_name_allocations) if one_to_one: return BijectiveHeaderPacker(header_format_class, structure, field_name_allocations) return SurjectiveHeaderPacker(header_format_class, structure, field_name_allocations)
Example #29
Source File: packer.py From segpy with GNU Affero General Public License v3.0 | 5 votes |
def __setstate__(self, state): if state['__version__'] != __version__: raise TypeError("Cannot unpickle {} version {} into version {}" .format(self.__class__.__name__, state['__version__'], __version__)) del state['__version__'] structure = Struct(state['_structure_format']) state['_structure'] = structure del state['_structure_format'] self.__dict__.update(state)
Example #30
Source File: primitives.py From aiozk with MIT License | 5 votes |
def parse(cls, buff, offset): """ Given a buffer and offset, returns the parsed value and new offset. Uses the ``format`` class attribute to unpack the data from the buffer and determine the used up number of bytes. """ primitive_struct = struct.Struct("!" + cls.fmt) value = primitive_struct.unpack_from(buff, offset)[0] offset += primitive_struct.size return value, offset