def pb2dict(obj):
    """
    Takes a ProtoBuf Message obj and convertes it to a dict.
    """
    try:
        adict = {}
        if not obj.IsInitialized():
            return None
        for field in obj.DESCRIPTOR.fields:
            if not getattr(obj, field.name):
                continue
            if not field.label == FD.LABEL_REPEATED:
                if not field.type == FD.TYPE_MESSAGE:
                    adict[field.name] = getattr(obj, field.name)
                else:
                    value = pb2dict(getattr(obj, field.name))
                    if value:
                        adict[field.name] = value
            else:
                if field.type == FD.TYPE_MESSAGE:
                    adict[field.name] = [pb2dict(v) for v in getattr(obj, field.name)]
                else:
                    adict[field.name] = [v for v in getattr(obj, field.name)]
        return adict
    except Exception as e:
        print(e)
        return obj

    Takes a ProtoBuf Message obj and convertes it to a dict.
    
example_rpc_server.py