繁体   English   中英

用于将网络中的字节列表(big-endian)字节顺序存储到文件(little-endian)的Python方法

[英]Python method for storing list of bytes in network (big-endian) byte order to file (little-endian)

我目前的任务是剖析包含P2P消息的tcpdump数据,我遇到的问题是我获取并写入x86机器上的文件。 我的怀疑是我有一个简单的字节序问题,我写入文件的字节。

我有一个字节列表,持有一段P2P视频读取和处理使用python-pcapy包BTW。

bytes = [14, 254, 23, 35, 34, 67, etc... ]

我正在寻找一种方法来存储这些字节,目前保存在我的Python应用程序的列表中的文件中。

目前我写的作品如下:

def writePiece(self, filename, pieceindex, bytes, ipsrc, ipdst, ts): 
    file = open(filename,"ab")
    # Iterate through bytes writing them to a file if don't have piece already 
    if not self.piecemap[ipdst].has_key(pieceindex):
        for byte in bytes: 
            file.write('%c' % byte)
        file.flush()
        self.procLog.info("Wrote (%d) bytes of piece (%d) to %s" % (len(bytes), pieceindex, filename))

    # Remember we have this piece now in case duplicates arrive 
    self.piecemap[ipdst][pieceindex] = True

    # TODO: Collect stats 
    file.close()

正如您在for循环中看到的那样,我将字节写入文件的顺序与从线程处理它们的顺序相同(即网络或大端顺序)。

可以说,作为片段有效载荷的视频在VLC中不能很好地播放:-D

我想我需要将它们转换为little-endian字节顺序,但我不确定在Python中处理它的最佳方法。

UPDATE

为我设计的解决方案(编写适当处理endian问题的P2P片段)是:

def writePiece(self, filename, pieceindex, bytes, ipsrc, ipdst, ts): 
    file = open(filename,"r+b")
    if not self.piecemap[ipdst].has_key(pieceindex):
        little = struct.pack('<'+'B'*len(bytes), *bytes) 
        # Seek to offset based on piece index 
        file.seek(pieceindex * self.piecesize)
        file.write(little)
        file.flush()
        self.procLog.info("Wrote (%d) bytes of piece (%d) to %s" % (len(bytes), pieceindex, filename))

    # Remember we have this piece now in case duplicates arrive 
    self.piecemap[ipdst][pieceindex] = True

    file.close()

解决方案的关键是怀疑使用Python结构模块,特别是:

    little = struct.pack('<'+'B'*len(bytes), *bytes) 

感谢那些回复了有用建议的人。

为了节省一些工作,你可能想使用bytearray (Python 2.6及更高版本):

b = [14, 254, 23, 35]
f = open("file", 'ab')
f.write(bytearray(b))

这样就可以将0-255的所有值转换为字节,而无需进行所有循环。

没有更多信息,我无法看到你的问题。 如果数据确实是按字节顺序的,则字节顺序不是问题,正如其他人所说的那样。

(顺便说一下,使用bytesfile作为变量名称并不好,因为它隐藏了同名的内置函数)。

您还可以使用array.array

from array import array
f.write(array('B', bytes))

代替

f.write(struct.pack('<'+'B'*len(bytes), *bytes))

当整理一点时

f.write(struct.pack('B' * len(bytes), *bytes))
# the < is redundant; there is NO ENDIANNESS ISSUE

如果len(字节)是“大”可能会更好

f.write(struct.pack('%dB' % len(bytes), *bytes)) 

这可能已经在Python File Slurp w / endian转换中得到了回答。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM