如何编码嵌套的Python Protobuf

问题描述 投票:0回答:1

被这个问题困扰了一段时间,把我剩下的头发拔了出来。

将非嵌套 Protobuf 从 Python 发送到 Java 以及 Java 发送到 Python,无需 WebSocket 的问题。我的问题是通过 WebSocket 发送嵌套版本。我相信我的问题是 Python 编码端。

感谢您的指导。

.proto 文件

message Response {
  // Reflect back to caller
  required string service_name = 1;

  // Reflect back to caller
  required string method_name = 2;

  // Who is responding
  required string client_id = 3;

  // Status Code
  required StatusCd status_cd = 4;

  // RPC response proto
  optional bytes response_proto = 5;

  // Was callback invoked
  optional bool callback = 6 [default = false];

  // Error, if any
  optional string error = 7;
  //optional string response_desc = 6;
}

message HeartbeatResult {
    required string service = 1;
    required string timestamp = 2;
    required float status_cd = 3;
    required string status_summary = 4;
}

心跳结果应该在 reponse_proto 字段中发送 响应 Protobuf 的。我可以用 Java 到 Java 来做到这一点,但用 Python 可以做到这一点 到 Java 不起作用。

我已经包含了 python 代码的两种变体。两者都不起作用。

   def GetHeartbeat(self):
    print "GetHeartbeat called"
    import time
    ts = time.time()
    import datetime
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    heartbeatResult = rpc_pb2.HeartbeatResult()
    heartbeatResult.service = "ALERT_SERVICE"
    heartbeatResult.timestamp = st
    heartbeatResult.status_cd = rpc_pb2.OK
    heartbeatResult.status_summary = "OK"

    response = rpc_pb2.Response()
    response.service_name = ""
    response.method_name = "SendHeartbeatResult"
    response.client_id = "ALERT_SERVICE"
    response.status_cd = rpc_pb2.OK 
    response.response_proto = str(heartbeatResult).encode('utf-8')

    self.sendMessage(response.SerializeToString())
    print "GetHeartbeat finished"

   def GetHeartbeat2(self):
    print "GetHeartbeat called"
    import time
    ts = time.time()
    import datetime
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    heartbeatResult = rpc_pb2.HeartbeatResult()
    heartbeatResult.service = "ALERT_SERVICE"
    heartbeatResult.timestamp = st
    heartbeatResult.status_cd = rpc_pb2.OK
    heartbeatResult.status_summary = "OK"

    response = rpc_pb2.Response()
    response.service_name = ""
    response.method_name = "SendHeartbeatResult"
    response.client_id = "ALERT_SERVICE"
    response.status_cd = rpc_pb2.OK 
    response.response_proto = heartbeatResult.SerializeToString()
    self.sendMessage(response.SerializeToString())
    print "GetHeartbeat finished"

Java服务器端的错误有:

(GetHeartbeat) Protocol message end-group tag did not match expected tag
and
(GetHeartbeat2)
Message: [org.java_websocket.exceptions.InvalidDataException: java.nio.charset.MalformedInputException: Input length = 1
    at org.java_websocket.util.Charsetfunctions.stringUtf8(Charsetfunctions.java:80)
    at org.java_websocket.WebSocketImpl.deliverMessage(WebSocketImpl.java:561)
    at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:328)
    at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:149)
    at org.java_websocket.server.WebSocketServer$WebSocketWorker.run(WebSocketServer.java:593)
Caused by: java.nio.charset.MalformedInputException: Input length = 1
    at java.nio.charset.CoderResult.throwException(CoderResult.java:277)
    at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:798)
    at org.java_websocket.util.Charsetfunctions.stringUtf8(Charsetfunctions.java:77)
java python websocket protocol-buffers
1个回答
0
投票

解决方案

也在protobuf组上发布了这个问题

感谢 Christopher Head 和 Ilia Mirkin 在 Google 群组中提供的意见

https://groups.google.com/forum/#!topic/protobuf/Cp7zWiWok9I

response.response_proto = base64.b64encode(heartbeatResult.SerializeToString())
self.sendMessage(response.SerializeToString())

仅供参考,Ilia 还建议对整个消息进行 Base64 编码,但这目前似乎有效。

© www.soinside.com 2019 - 2024. All rights reserved.