[LINUX] Run iperf with python

Introduction

I wanted to run iperf with python instead of a shell script, but I didn't have a Japanese document, so I'll leave a simple way to do it.

environment

Ubuntu 14.04 python 3.5.1

Preparation

First, install the python wrapper using pip. pip install iperf3 It is also possible to install directly from the github repository without using pip.

git clone https://github.com/thiezn/iperf3-python.git
cd iperf3-python
python3 setup.py install

How to use

Server side code

iperf_server.py


#!/usr/bin/env python3

import iperf3

server = iperf3.Server()
print('Running server: {0}:{1}'.format(server.bind_address, server.port))

while True:
    result = server.run()

    if result.error:
        print(result.error)
    else:
        print('')
        print('Test results from {0}:{1}'.format(result.remote_host,
                                                 result.remote_port))
        print('  started at         {0}'.format(result.time))
        print('  bytes received     {0}'.format(result.received_bytes))

        print('Average transmitted received in all sorts of networky formats:')
        print('  bits per second      (bps)   {0}'.format(result.received_bps))
        print('  Kilobits per second  (kbps)  {0}'.format(result.received_kbps))
        print('  Megabits per second  (Mbps)  {0}'.format(result.received_Mbps))
        print('  KiloBytes per second (kB/s)  {0}'.format(result.received_kB_s))
        print('  MegaBytes per second (MB/s)  {0}'.format(result.received_MB_s))
        print('')

Then the client side code

iperf_client.py


#!/usr/bin/env python3

import iperf3

client = iperf3.Client()
client.duration = 10 # Measurement time [sec]
client.server_hostname = '192.168.1.1' # Server's IP address

print('Connecting to {0}:{1}'.format(client.server_hostname, client.port))
result = client.run()

if result.error:
    print(result.error)
else:
    print('')
    print('Test completed:')
    print('  started at         {0}'.format(result.time))
    print('  bytes transmitted  {0}'.format(result.sent_bytes))
    print('  retransmits        {0}'.format(result.retransmits))
    print('  avg cpu load       {0}%\n'.format(result.local_cpu_total))

    print('Average transmitted data in all sorts of networky formats:')
    print('  bits per second      (bps)   {0}'.format(result.sent_bps))
    print('  Kilobits per second  (kbps)  {0}'.format(result.sent_kbps))
    print('  Megabits per second  (Mbps)  {0}'.format(result.sent_Mbps))
    print('  KiloBytes per second (kB/s)  {0}'.format(result.sent_kB_s))
    print('  MegaBytes per second (MB/s)  {0}'.format(result.sent_MB_s))

in conclusion

With this, you can measure the throughput of the network for the time being. The server (192.168.1.1) and the client make measurements using iperf3 for 10 seconds. Although not changed in this code, it is possible to specify the port number and UDP / TCP as well as iperf executed on the command line.

References

https://pypi.python.org/pypi/iperf3 https://github.com/thiezn/iperf3-python

Recommended Posts

Run iperf with python
Run Python with VBA
Run prepDE.py with python3
Run Blender with python
Run Python with CloudFlash (arm926ej-s)
Let's run Excel with Python
Run Label with tkinter [Python]
Run DHT22 with RasPi + Python
Run mruby with Python or Blender
Run XGBoost with Cloud Dataflow (Python)
Run Aprili from Python with Orange
Run python3 Django1.9 with mod_wsgi (deploy)
Until you run python with apache
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Integrate with Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Run servo with Python on ESP32 (Windows)
Run a Python web application with Docker
Serial communication with Python
Zip, unzip with python
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Sequential search with Python
"Object-oriented" learning with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learn Python with ChemTHEATER
Collecting tweets with Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Run python from excel
Python> Run with run-time arguments> Use import argparse
Operate Kinesis with Python
Getting Started with Python
Zundko getter with python
Handle Excel with python