How can print protobuf message with custom format in python?

61 Views Asked by At

Here my code

# protobuf message
# message Health {
#     bytes mac_addr = 1;
#     uint32 device_uptime = 2;
#     uint32 teapot_uptime = 3;
#     uint32 start_count = 4;
#     uint32 start_reason = 5;
#     uint32 cpu_usage = 6;
#     uint32 cpu_temp = 7;
#     uint32 free_memory = 8;
# }


if __name__ == "__main__":

    health = health_pb2.Health()
    health.device_uptime = 17
    health.mac_addr = bytes.fromhex("000e1f8492db")

    print(health)

output of below code:

mac_addr: "\000\016\037\204\222\333"
device_uptime: 17

What i desired output:

mac_addr: "00:0e:1f:84:92:db"
device_uptime: 17

I have a lot of protobuf message contains mac address and ip adress fields. When i want to print those message is not human readable. How can solve those problem without writing manual print function for each message?

0

There are 0 best solutions below