【问题标题】:Python SerializeToString and Unmarshal in Go ProtobufGo Protobuf 中的 Python SerializeToString 和 Unmarshal
【发布时间】:2021-08-05 13:16:29
【问题描述】:

我需要将 protobuf 消息从 python 发送到 Go 系统。在 python 端,我调用 SerializeToString() 并将这个字符串作为 http 响应的主体发送。在项目的 go 方面,我在阅读响应正文后致电Unmarshal()。但这样做时我收到proto: cannot parse invalid wire-format data 错误。

请注意反过来,当我从 Go 向 Python 发送请求时,Go 中的请求编组和 Python 中的解组工作没有任何问题。

有什么想法吗?谢谢你

【问题讨论】:

  • SerializeToString()的输出是什么
  • Unmarshal() 是正确使用的函数。不幸的是,需要更多信息来调试问题。
  • 嗨,你找到解决办法了吗?

标签: python go protocol-buffers proto


【解决方案1】:

Python SerializeToString() 函数是一种创建 protobuf 消息的二进制编码字符串表示形式的方法,例如writing messages to a file 但是这不是通过网络发送消息的方式。

IIUC 你有 Golang 客户端和 Python 服务器吗?

Golang 客户端应该调用服务方法。某种形式的东西:

resp,err := client.SomeMethod(ctx,&pb.SomeMethodRequest{...})

Python 服务器应该有一个 service 方法的实现。某种形式的东西:

def SomeMethod(self, request, context)

注意 gRPC 确保在 Golang 客户端上传递给 SomeMethod&pb.SomeMethodRequest{...} 在 Python 服务器方法中显示为 request

在客户端和服务器中,您将使用由 protoc 编译器生成的 Golang 类型和 Python 类(例如 pb.SomeMethodRequest)。

【讨论】:

  • 感谢您的评论。但是 python 端是一个在 OpenFaaS 中运行的函数。我仍然想在 python 函数中使用 proto 对象。所以我不能真正在python端实现gRPC-rpc函数调用。
  • 所以你希望 Golang 客户端使用常规 HTTP 和 Python 服务器?如果是这样,Golang 客户端应该简单地ParseFromString Python 服务器返回的消息体。见Reading a Message
猜你喜欢
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多