1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonでiperfを動かす

Posted at

はじめに

iperfをシェルスクリプトではなく、pythonで動かしたくなったが、日本語のドキュメントがなかったため、簡単にやりかたを残しておきます。

環境

Ubuntu 14.04
python 3.5.1

準備

まずpython wrapperをpipを使ってインストール。
pip install iperf3
pipを使わずにgithubのレポジトリから直接インストールすることも可能。

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

使い方

サーバ側のコード

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('')

次にクライアント側のコード

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))

おわりに

これで取り敢えず、ネットワークのスループットを計測することができる。
サーバ(192.168.1.1)とクライアントが10秒間iperf3を使って測定を行う。
今回のコードでは変更していないが、コマンドライン上で実行するiperfと同様に、ポート番号やUDP/TCPの指定をすることも可能。

参考文献

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

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?