Skip to main content

A python package containing gnmi proto specification and generated python code.

Project description

gNMI Protobuf

PyPI version Python Versions Code style: black

This project aims to be a base building block for gNMI projects written in Python.

The process of building pythonic libraries and applications using gRPC and Protocol Buffers have been fragmented. This often means that a developer needs to copy over proto files, generate Python source from these using protoc and use them in-tree for their project. There already exists several projects built in this fashion. While functional, these can be hard to reuse or maintain, often times resulting stale code and no versioning.

gnmi-proto builds on top of the improvement already done by betterproto and in turn by the grpclib library. Here, we make available, as versioned packages, code generated from gNMI protocol buffers.

The default implementation makes use of the betterproto protoc plugin to generate clean modern code. In addition, this also provides a gnmi.proto.legacy module exposing code generated by protoc using the the in-built Python generator.

Example Usage

Client

The following code expects a server at 127.0.0.1:9339 with the test configuration. Refer to gNMI Target Server section in CONTRIBUTING.md for information on how to set it up.

Using betterproto and grpclib

import gnmi.proto
import grpclib.client


async def main():
    channel = grpclib.client.Channel(host="127.0.0.1", port=9339, ssl=None)
    service = gnmi.proto.gNMIStub(
        channel, metadata={"username": "admin", "password": "secret"}
    )

    response = await service.capabilities()
    print(response.to_json(indent=2))

    response = await service.get(
        path=[gnmi.proto.Path(elem=[gnmi.proto.PathElem(name="interfaces")])],
    )
    print(response.to_json(indent=2))


if __name__ == "__main__":
    import asyncio
    asyncio.run(main())

Using vanilla grpc

import gnmi.proto.legacy
import grpc


def main():
    channel = grpc.insecure_channel("127.0.0.1:9339")
    metadata = [("username", "admin"), ("password", "secret")]
    service = gnmi.proto.legacy.gNMIStub(channel)

    response = service.Capabilities(gnmi.proto.legacy.CapabilityRequest())
    print(response)

    response = service.Get(
        gnmi.proto.legacy.GetRequest(
            path=[
                gnmi.proto.legacy.Path(
                    elem=[gnmi.proto.legacy.PathElem(name="interfaces")]
                )
            ],
        ),
        metadata=metadata,
    )
    print(response)


if __name__ == "__main__":
    main()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gnmi-proto-0.1.0a4.tar.gz (38.0 kB view hashes)

Uploaded Source

Built Distribution

gnmi_proto-0.1.0a4-py3-none-any.whl (43.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page