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
.
![](https://www.programcreek.com/common/static/images/search.png)
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: 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 #3
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 #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): """ 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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
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 #23
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 #24
Source File: ioctl.py From buttersink with GNU General Public License v3.0 | 5 votes |
def __init__(self, *fields, **keyArgs): """ Initialize. """ (names, formats, types) = unzip([self._parseDefinition(*f) for f in fields]) self._Tuple = collections.namedtuple("StructureTuple", names) self._fmt = "".join(formats) self._packed = keyArgs.get('packed', True) self._struct = struct.Struct("=" + self._fmt if self._packed else self._fmt) self._types = collections.OrderedDict(zip(names, types))
Example #25
Source File: control_record.py From aiokafka with Apache License 2.0 | 5 votes |
def parse(cls, data: bytes, _schema=struct.Struct(">HH")): version, type_ = _schema.unpack_from(data) return cls(version, type_)
Example #26
Source File: serializer.py From CoAPthon3 with MIT License | 5 votes |
def read_option_value_len_from_byte(byte, pos, values): """ Calculates the value and length used in the extended option fields. :param byte: 1-byte option header value. :return: the value and length, calculated from the header including the extended fields. """ h_nibble = (byte & 0xF0) >> 4 l_nibble = byte & 0x0F value = 0 length = 0 if h_nibble <= 12: value = h_nibble elif h_nibble == 13: value = struct.unpack("!B", values[pos].to_bytes(1, "big"))[0] + 13 pos += 1 elif h_nibble == 14: s = struct.Struct("!H") value = s.unpack_from(values[pos:].to_bytes(2, "big"))[0] + 269 pos += 2 else: raise AttributeError("Unsupported option number nibble " + str(h_nibble)) if l_nibble <= 12: length = l_nibble elif l_nibble == 13: length = struct.unpack("!B", values[pos].to_bytes(1, "big"))[0] + 13 pos += 1 elif l_nibble == 14: length = s.unpack_from(values[pos:].to_bytes(2, "big"))[0] + 269 pos += 2 else: raise AttributeError("Unsupported option length nibble " + str(l_nibble)) return value, length, pos
Example #27
Source File: connections.py From satori with Apache License 2.0 | 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 #28
Source File: core.py From debin with Apache License 2.0 | 5 votes |
def __init__(self, name, endianity, format): if endianity not in (">", "<", "="): raise ValueError("endianity must be be '=', '<', or '>'", endianity) if len(format) != 1: raise ValueError("must specify one and only one format char") self.packer = Packer(endianity + format) StaticField.__init__(self, name, self.packer.size)
Example #29
Source File: core.py From debin with Apache License 2.0 | 5 votes |
def __setstate__(self, attrs): attrs["packer"] = Packer(attrs["packer"]) return StaticField.__setstate__(attrs)
Example #30
Source File: heartbleed.py From fuzzdb-collect with GNU General Public License v3.0 | 5 votes |
def skip_server_handshake(sock, timeout): end_time = time.time() + timeout hs_struct = struct.Struct(b'!BBH') for i in range(0, 5): record, error = read_record(sock, timeout) timeout = end_time - time.time() if not record: raise Failure('Unexpected server handshake! ' + str(error)) content_type, sslver_num, fragment = record if content_type != 22: raise Failure('Expected handshake type, got ' + str(content_type)) sslver = '{0:02x} {1:02x}'.format(sslver_num >> 8, sslver_num & 0xFF) off = 0 # Records may consist of multiple handshake messages while off + hs_struct.size <= len(fragment): hs_type, len_high, len_low = hs_struct.unpack_from(fragment, off) if off + len_low > len(fragment): raise Failure('Illegal handshake length!') off += hs_struct.size + len_low # Server handshake is complete after ServerHelloDone if hs_type == 14: # Ready to check for vulnerability return sslver raise Failure('Too many handshake messages')