0

so I am trying to send over serialized protobuf messages over SSH and deserialize them on my local machine. However, for some reason when I send over the serialized string, I get a DecodeError saying that I am trying to deserialize a Truncated Message.

The way I have this setup is I have two scripts, one being run on the server through Popen and ssh on my local script. The script being run on the server is simply printing the message.SerializeToString() to stdout and then I am collecting that output through Popen.stdout on my local script. Here are some code snippets:

Local Machine Script:

magic_str = 'this_is_magical'
ssh = subprocess.Popen('ssh xxx.xxx.xxx.xxx logsearch.py', stdout=subprocess.PIPE)
for line in ssh.stdout:
  #this is just the specific protobuf message structure I created
  l = log_pb2.LogLine()

  #restore the internal newlines, discussed below
  l.ParseFromString(line.replace(magic_str, '\n'))

Server Script (logsearch.py):

  #get some LogLine object named l, and we replace the internal newlines with
  #magic_str because it messes up reading line by line in the local script
  magic_str = 'this_is_magical'
  print l.SerializeToString().replace('\n', magic_str)

Thank you in advance for any help you can provide me with, it is much appreciated. If any extra information is needed or if anything is unclear, please let me know and I will make sure to clarify.

4

2 回答 2

0

要做的第一件事是忘记实际尝试反序列化,只需检查:在运行魔术替换后,您是否取回了正确的二进制文件。检查它与原始文件完全相同的二进制文件(不是文本)。

如果您从根本上获取文本,则在传输之前简单地对数据进行 base-64 编码可能更明智,然后在另一侧对其进行 base-64 解码。那么您将无需担心任何替代品。

于 2013-09-16T20:27:46.233 回答
0

我不确定我是否完全理解您在这里所做的事情,但是您确定要为 ssh.stdout 中的每一创建一个新的 LogLine 消息吗?Protobuf 反序列化要求整个消息能够正确反序列化。就像是

ssh_data = #... parse all the data received from the pipe (not line for line)
l = log_pb2.LogLine()
l.ParseFromString(ssh_data.replace(magic_str, '\n'))
于 2013-09-16T19:55:27.033 回答