Python protobuf decode base64 string

Viewed 2830

I am trying to get JSON data from encrypted base64 string. I have created my proto file like below

syntax = "proto2";

message ArtifactList {
    repeated Artifact artifacts = 1;
}

message Artifact {
    required string id = 1;
    required uint64 type_id = 2;
    required string uri = 3;
}

After that, I have generated python files using the proto command. I am trying to decrypt the base64 string like below.

import message_pb2

import base64
data = base64.b64decode("AAAAAA8KDQgTEBUgBCjln62lxS6AAAAAD2dycGMtc3RhdHVzOjANCg==")

s = str(data)
message_pb2.ArtifactList.ParseFromString(s)

But I am getting the below error.

Traceback (most recent call last):
  File "app.py", line 7, in <module>
    message_pb2.ArtifactList.ParseFromString(s)
TypeError: descriptor 'ParseFromString' requires a 'google.protobuf.pyext._message.CMessage' object but received a 'str'

I am a newbie for protobuf. I couldn't find a solution to fix this issue.

Could anyone help to fix this problem?

Thanks in advance.

2 Answers

There are two issues.

  1. ParseFromString is a method of an ArtifactList instance
  2. ParseFromString takes a byte-like object, not str, as parameter
>>>import message_pb2
>>>import base64
>>>data = base64.b64decode("AAAAAA8KDQgTEBUgBCjln62lxS6AAAAAD2dycGMtc3RhdHVzOjANCg==")
>>>m=message_pb2.ArtifactList()
>>>m.ParseFromString(data)
>>>m.artifacts
<google.protobuf.pyext._message.RepeatedCompositeContainer object at 0x7fd09a937d68>

ParseFromString is a method on an protobuf Message instance.

Try:

message = message_pb2.ArtifactList()
message.ParseFromString(s)
Related