generate python classes from application/x-protobuf file

时间:2015-07-29 00:29:44

标签: python protocol-buffers

I have a python script that receives a application/x-protobuf file from youtube. This part of the file

deployment.log=true
deployment.user.logdir=C\:\\tmp

I want to generate python classes from this file. I used protoc to decode the file

*youtubei#playerResponse
\862\94

yt_ad1\83
e~11200142,901816,936105,9407053,9407664,9407715,9408142,9410705,9412913,9415294,9416137,9417116,9417192,9417455,9418117,94182142\88\83
e~11200142,901816,936105,9407053,9407664,9407715,9408142,9410705,9412913,9415294,9416137,9417116,9417192,9417455,9418117,94182142\D6+
InnerTubeBuildLabelyoutube_20150727_RC2
InnerTubeChangelist99168778\83
e~11200142,901816,936105,9407053,9407664,9407715,9408142,9410705,9412913,9415294,9416137,9417116,9417192,9417455,9418117,94182142\88&
innertube.build.changelist99168778-
innertube.build.labelyoutube_20150727_RC2'
innertube.build.timestamp
1437996969E
!innertube.build.variants.checksum 47cbe83e1d9f5a44654ab7896473362e
innertube.client_name3!
innertube.client_version10.28z\BC\9A\EF\CA\DC\A2\80\C8 \E0](\A8F0\88\A48\98u@\B0\EAM\CD\CCL?U\CD\CCL?]\00\00@?`2h\E42p\C0>x\80\88\C4\90\98\A0\00\AD\00\00\00\00\B0\00\B8\00\C0\C8Ќ\D0\00\D8\E0\E8\C7\F0d\F8\00\80\90\00\98\00\C0\00\C8\A0\A8\00\B5\00\00\A0B\B8\00\D8\00\E0\00\E8\00\F0\F0\F8\80\00\88\00\90\00\B2ϭ\D3
\00\00 \C1\A2\DFޜ\00Z\AD

Then I used this command to generate the classes

cat binay_file | protoc --decode_raw > decoded_file

however this command always returns "Expected top-level statement (e.g. "message")." error.

1 个答案:

答案 0 :(得分:1)

protoc的输入是.proto源文件,声明协议的整体结构。看起来您正在尝试将实际消息用作输入。这不起作用 - 这些不是一回事。

没有自动方法对消息实例中的.proto文件进行反向工程,因为编码消息不包含类型名称或字段名称等内容,并且仅包含有关实际字段类型的有限信息。您可以使用--decode_raw的输出来猜测原始.proto文件,但这是一项需要人工分析的逆向工程任务,而不是程序可以完成的任务。