Skip to main content

The Snappi Open Traffic Generator Python Package

Project description

snappi

license Project Status: Active – The project has reached a stable, usable state and is being actively developed. Build Total alerts Language grade: Python pypi python

Test scripts written in snappi, an auto-generated python SDK, can be executed against any traffic generator conforming to Open Traffic Generator API.

Ixia-c is one such reference implementation of Open Traffic Generator API.

The repository is under active development and is subject to updates. All efforts will be made to keep the updates backwards compatible.

Setup Client

python -m pip install --upgrade snappi 

Start Testing

import datetime
import time
import snappi
import pytest


@pytest.mark.example
def test_quickstart():
    # Create a new API handle to make API calls against OTG
    # with HTTP as default transport protocol
    api = snappi.api(location="https://localhost:8443")

    # Create a new traffic configuration that will be set on OTG
    config = api.config()

    # Add a test port to the configuration
    ptx = config.ports.add(name="ptx", location="veth-a")

    # Configure a flow and set previously created test port as one of endpoints
    flow = config.flows.add(name="flow")
    flow.tx_rx.port.tx_name = ptx.name
    # and enable tracking flow metrics
    flow.metrics.enable = True

    # Configure number of packets to transmit for previously configured flow
    flow.duration.fixed_packets.packets = 100
    # and fixed byte size of all packets in the flow
    flow.size.fixed = 128

    # Configure protocol headers for all packets in the flow
    eth, ip, udp, cus = flow.packet.ethernet().ipv4().udp().custom()

    eth.src.value = "00:11:22:33:44:55"
    eth.dst.value = "00:11:22:33:44:66"

    ip.src.value = "10.1.1.1"
    ip.dst.value = "20.1.1.1"

    # Configure repeating patterns for source and destination UDP ports
    udp.src_port.values = [5010, 5015, 5020, 5025, 5030]
    udp.dst_port.increment.start = 6010
    udp.dst_port.increment.step = 5
    udp.dst_port.increment.count = 5

    # Configure custom bytes (hex string) in payload
    cus.bytes = "".join([hex(c)[2:] for c in b"..QUICKSTART SNAPPI.."])

    # Optionally, print JSON representation of config
    print("Configuration: ", config.serialize(encoding=config.JSON))

    # Push traffic configuration constructed so far to OTG
    api.set_config(config)

    # Start transmitting the packets from configured flow
    ts = api.transmit_state()
    ts.state = ts.START
    api.set_transmit_state(ts)

    # Fetch metrics for configured flow
    req = api.metrics_request()
    req.flow.flow_names = [flow.name]
    # and keep polling until either expectation is met or deadline exceeds
    start = datetime.datetime.now()
    while True:
        metrics = api.get_metrics(req)
        if (datetime.datetime.now() - start).seconds > 10:
            raise Exception("deadline exceeded")
        # print YAML representation of flow metrics
        print(metrics)
        if metrics.flow_metrics[0].transmit == metrics.flow_metrics[0].STOPPED:
            break
        time.sleep(0.1)

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

snappi-1.16.0.tar.gz (417.5 kB view details)

Uploaded Source

Built Distribution

snappi-1.16.0-py2.py3-none-any.whl (420.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file snappi-1.16.0.tar.gz.

File metadata

  • Download URL: snappi-1.16.0.tar.gz
  • Upload date:
  • Size: 417.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.20 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

File hashes

Hashes for snappi-1.16.0.tar.gz
Algorithm Hash digest
SHA256 f0fb085ffa22c0466e57204172dd895042a1428cb0e152bdecae19303d17a575
MD5 213ae8428cbe6230d7294d87ed28ad08
BLAKE2b-256 4a8c4877815a65b2e1f123ca3821c32e32618070bf91308af9f26cac224bd329

See more details on using hashes here.

File details

Details for the file snappi-1.16.0-py2.py3-none-any.whl.

File metadata

  • Download URL: snappi-1.16.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 420.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.20 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

File hashes

Hashes for snappi-1.16.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 dc7054cab32c82f7454d000b78b8702c68f1a7da0f850dd9695ac33d68280028
MD5 f23a4b4e947d274fa05c3b5123e43338
BLAKE2b-256 7a17d867bc22159e6401447467cc05187b00df2c5ef4c43ad9d86916be6935fe

See more details on using hashes here.

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