How to parse C++ protobuf binary data by python protobuf?
<p>I use C++ protobuf to serialize data to string.</p>

<pre><code>/***** cpp code *****/
string serialized_data;
message_cpp.SerializeToString(&amp;serialized_data);
</code></pre>

<p><strong>Question:</strong> can I parse <code>serialized_data</code> in python? and how?</p>

<p>I've tried the following code, but it did not work.</p>

<pre><code>##### python code
message_python = foo.ParseFromString(serialized_data)
print message_python
</code></pre>

<p>But I get <code>None</code> as the output of <code>print message_python</code>. I've also tried </p>

<pre><code>##### python code
message_python = foo.MergeFromString(serialized_data)
print message_python
</code></pre>

<p>But I get the length of string <code>serialized_data</code> as the output of <code>print message_python</code>, i.e. <code>message_python == len(serialized_data)</code>. This result agrees with <a href="https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.message.Message-class#MergeFromString" rel="nofollow">python protobuf API</a>.</p>

<p>Does this means that I cannot parse the binary data in python, which are serialized in C++?</p>

<p><strong>Update:</strong></p>

<p>My <em>goal</em>ï¼ C++ server always generates image stream and image is sent to a python server.</p>

<p>Here are my whole codes:</p>

<p>.proto file:</p>

<pre><code>message MyImage{
    repeated int32 width = 1;
    repeated int32 height = 2;
    repeated bytes image = 3;
}
</code></pre>

<p>C++ server:</p>

<pre><code>zmq::context_t context(1);
zmq::socket_t socket(context, ZMQ_REP);
socket.bind("tcp://localhost:5555");

MyImage message_cpp;
// message_cpp.add_image(), add_width() and add_height() here.

string serialized_data;
message_cpp.SerializeToString(&amp;serialized_data);
int counter = 3;
while (counter &gt; 0) {
    zmq::message_t request;
    socket.recv(&amp;request);
    std::string replyMessage = std::string(static_cast&lt;char *&gt;(request.data()),
                                           request.size());
    std::cout &lt;&lt; "Recived from client: " + replyMessage &lt;&lt; std::endl;
    sleep(1);
    zmq::message_t reply(serialized_data.size());
    memcpy((void*) reply.data(), serialized_data.data(), serialized_data.size());
    std::cout &lt;&lt; "---length of message to client: " &lt;&lt; 
                  reply.size() &lt;&lt; std::endl;
    socket.send(reply);

    counter --;
}
</code></pre>

<p>python client:</p>

<pre><code>context = zmq.Context()
socket = context.socket(zmq.REQ)
port = "5555"
socket.connect("tcp://localhost:%s" %port)
print "Connecting to server..."
foo = my_image_pb2.MyImage()

for i in range(3):
    socket.send("hello from python")
    serialized_data = socket.recv()
    message_python = foo.ParseFromString(serialized_data)
    print "length of message from server:", len(serialized_data),"; type:", type(message)
    print "-----", message_python
</code></pre>

<p>This is the result:</p>

<p>server:</p>

<p><a href="http://i.stack.imgur.com/dqmpz.png" rel="nofollow"><img src="http://i.stack.imgur.com/dqmpz.png" alt="server"></a></p>

<p>client:</p>

<p><a href="http://i.stack.imgur.com/RjPG5.png" rel="nofollow"><img src="http://i.stack.imgur.com/RjPG5.png" alt="client"></a></p>

<p>Why the <code>foo</code> is <code>None</code>, not a class? Any idea about how to fix it?</p>

<p><code>ParseFromString</code> parses <em>into</em> the object it is called on. It doesn't return anything. Use it like:</p>

<pre><code>message = MyMessage()
message.ParseFromString(data)
print message
</code></pre>

python c++ protocol-buffers