Why is my Protobuf message truncated only when sending from a Windows window to a Linux box?

I use a simple, synchronous Python server and client combination to send Google Protobuf messages over a TCP connection. The server runs on Windows XP using Python 2.5.4 and Google Protobuf 2.4.1. The client runs on a Ubuntu 12.04 server using Python 2.7.3 and Google Protobuf 2.4.1.

When I run the server on XP computer and I try to talk to him by a client on a Ubuntu machine, the server parses the message perfectly with the request and sends a response message just fine, but the client encounters google.protobuf.message.DecodeError: Truncated message error.

If I run both the client and the server on an XP computer, I do not encounter this error and do not encounter an error if I run the server on the Ubuntu machine and on the client on the XP machine.

It looks like this may have something to do with the Protobuf message itself ... if I use a very simple Protobuf message, similar to the one in the example below, it seems that everything is working fine. However, as soon as I start using the actual Protobuf message that we use in production, I see an error. It still seems strange, although I only get the error when the server is on Windows and the client is on Ubuntu.

Any ideas what could happen ?! Given some environmental limitations, I have to stick with Python 2.5 on a Windows machine and Google Protobuf 2.4.1.

Protobuf.proto example

 message Foo { required string name = 1; } message Bar { required string name = 1; } message Sucka { required string name = 1; repeated Foo foos = 2; repeated Bar bars = 3; } message Result { enum Exception { NONE = 0; GENERIC = 1; } required Exception exception = 1; optional string message = 2; optional Sucka sucka = 3; } 

server.py

 import signal, socket, struct from protobuf import Sucka, Result class TestServer(): def __init__(self, host = '0.0.0.0', port = 9989): self.host = host self.port = port self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.bind((self.host, self.port)) self.socket.listen(1) self.running = False self.conn = None self.run() def run(self): def handle_exit(signum, stack): print 'Shutting down server in response to signal %s' % (str(signum)) self.running = False signal.signal(signal.SIGINT, handle_exit) signal.signal(signal.SIGTERM, handle_exit) print 'Waiting on a connection' self.running = True while self.running: try: self.conn, addr = self.socket.accept() print 'Connection from:', addr size_t = self.conn.recv(4) size = struct.unpack('!I', size_t) data = self.conn.recv(size[0]) if not data: raise Exception('No data... did the client close the connection?') result = Result() try: sucka = Sucka() sucka.ParseFromString(data) result.exception = result.NONE result.sucka.CopyFrom(sucka) except Exception, ex: print str(ex) result.exception = result.GENERIC result.message = 'Exception: %s' % (str(ex)) finally: if self.conn: self.conn.close() self.socket.close() if __name__ == '__main__': s = TestServer() 

client.py

 import socket, struct, time from protobuf import Sucka, Result class TestClient(): def __init__(self, sucka, host = '127.0.0.1', port = 9989): self.addr = (host, port) self.__call(sucka) def __call(self, sucka): size = sucka.ByteSize() size_t = struct.pack('!I', size) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(self.addr) sock.send(size_t) sock.send(sucka.SerializeToString()) size_t = sock.recv(4) size = struct.unpack('!I', size_t) data = sock.recv(size[0]) result = Result() result.ParseFromString(data) if result.exception != Result.NONE: raise Exception(result.message) print str(result.sucka) if __name__ == '__main__': import sys, os sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) from protobuf import Sucka sucka = Sucka() sucka.name = 'testing' for i in range(0, 25): foo = sucka.foos.add() foo.name = 'foo-%i-tester' % i for i in range(0, 25): bar = sucka.bars.add() bar.name = 'bar-%i-tester' % i c = TestClient(sucka, '127.0.0.1', 9989) 
+6
source share

Source: https://habr.com/ru/post/953597/


All Articles