首页 > 解决方案 > Python Implementing protobuf --decode-raw in python

问题描述

I am dealing with the protobuf protocol and I encounter the need of decoding messages of unknown fields and types. I know protoc --decode_raw does a good job at that (alas not precise but good enough).

I was thinking about running protoc --decode-raw in a shell and let Python read its content, parsing it as a dictionary, but I figure it is the last resort of implementation.

Is there a Pythonic method of implementing the same functionality?

标签: pythonprotocol-buffersprotobuf-python

解决方案


我也有同样的需求,但据我所知,没有官方的 API。有一些内部方法,例如访问msg._unknown_fields空消息和internal.decoder。但这些不是官方 API 的一部分,并且因版本而异。

如果 shell 方法太老套,最好的办法是使用自定义 Python 代码根据文档实现解码。


推荐阅读