簡體   English   中英

從C#向Python發送Protobuf消息

[英]Sending Protobuf message from C# to python

我在proto文件中有一個帶有兩個字段的簡單類:(proto3)

enum MaestroMsgType
{
    EVAL = 0;
    GET_ACK = 1;
    GET_AN = 2;
}

message MaestroMsg
{
    MaestroMsgType msgType = 1;
    string maestroMsg = 2;
}

我正在嘗試使用netMQ將類形式的C#發送(使用netMQ)到python。 但是在python中,當試圖使數據恢復為類格式時,它會失敗。

在C#中發送:

  MaestroMsg maestroMsg = new MaestroMsg
  {
       MaestroMsg_ = "someMessage",
       MsgType = MaestroMsgType.GET_AN,
   };
   string messageToSend = maestroMsg.ToString();
   NetMQMessage msg = new NetMQMessage();
   msg.Append(messageToSend);
   _pubSocket.SendMultipartMessage(msg);

python中接收消息的代碼:

 received_message = sub_socket.recv_multipart()
 maestroMsg_object = MaestroMsg()
 maestroMsg_object.ParseFromString(received_message.encode())

我收到此錯誤消息:

 google.protobuf.message.DecodeError: Error parsing message

我不知道我在做什么錯。 感謝您的回答。

因此,感謝@MarcGravell,答案就是對API的錯誤使用:

C#:

MaestroMsg maestroMsg = new MaestroMsg
{
    MaestroMsg_ = maestroMsgStr,
    MsgType = maestroMsgType,
};

var messageToSend = maestroMsg.ToByteArray();
NetMQMessage msg = new NetMQMessage();
msg.Append(messageToSend);

_pubSocket.SendMultipartMessage(MSG);

蟒蛇:

received_message = sub_socket.recv_multipart()
meastro_object = MaestroMsg()
meastro_object.ParseFromString(received_message)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM