python解析二进制数据

时间:2014-09-18 13:12:49

标签: c# python serialization binary

我在(windows)中有一个以二进制格式发送日志的应用程序。 将其转换为字符串的c#代码是:

public static CounterSampleCollection Deserialize(BinaryReader binaryReader)
{
  string name = binaryReader.ReadString();  // counter name 
  short valueCount = binaryReader.ReadInt16();  // number of counter values

   var sampleCollection = new CounterSampleCollection(name);
   for (int i = 0; i < valueCount; i++)
   {
    // each counter value consists of a timestamp + the actual value 
    long binaryTimeStamp = binaryReader.ReadInt64();
    DateTime timeStamp = DateTime.FromBinary(binaryTimeStamp);
    float value = binaryReader.ReadSingle();

     sampleCollection.Add(new CounterSample(timeStamp, value));
  }
  return sampleCollection;
}

我有一个正在侦听端口的python udp套接字,但不知道如何将我收到的二进制数据转换为字符串,以便我可以进一步解析它。

任何python专家都可以帮我将该函数转换为python函数,这样我就可以将收到的数据转换成python。

到目前为止我的代码:

import socket

UDP_IP = "0.0.0.0"
UDP_PORT = 40001

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(8192) # buffer size is 8192 bytes
    print "[+] : ", data
    // this prints the binary 
    // convert the data to strings  ?? 

2 个答案:

答案 0 :(得分:0)

我使用struct来解包二进制数据。 https://docs.python.org/2/library/struct.html 这是我用来从静态文件中解压缩数据的一个例子。

 import struct    
 comp = open(traceFile, 'rb')
 aData = comp.read()
 s = struct.Struct('>' +' i i i f f f d i H H')
 sSize = s.size
 for n in range(0, len(aData), sSize):
     print s.unpack(aData[n:n+sSize])

答案 1 :(得分:0)

以下介绍了从套接字读取的示例:

http://www.binarytides.com/receive-full-data-with-the-recv-socket-function-in-python/

该引用的片段为您提供了一些编写所需Python代码的工具。该片段使用try ... except子句和sleep()函数。该参考包含其他很好的提示。但问题的关键是二进制数据自然地转换为python字符串。

while 1:
    #recv something
    try:
        data = the_socket.recv(8192)
        if data:
            total_data.append(data)
            #change the beginning time for measurement
            begin=time.time()
        else:
            #sleep for sometime to indicate a gap
            time.sleep(0.1)
    except:
        pass

#join all parts to make final string
s = ''.join(total_data)   # join accepts type str, so binary string is converted

在你有字符串“s”之后,你需要根据(1)你拥有的数据对的分隔符,(2)日期之间的分隔符和(3)日期字段的格式进行解析。我不知道你的二进制字符串是什么样的,所以我将简要描述一些你可能会使用的代码:

results = []
from datetime import datetime
pairs = s.split('\n')    # assume that the pairs are linefeed-separated
for pair in pairs:
    sdate, scount = pair.split(',')    # assume that a pair is separated by a comma
    timestamp = datetime.strptime(sdate, "%Y-%m-%d %H:%M:%S.%f")   # format must match sdate
    count = int(scount)
    results.append(timestamp, count)
return results