Ich wollte iperf mit Python anstelle eines Shell-Skripts ausführen, hatte aber kein japanisches Dokument, daher lasse ich einen einfachen Weg, dies zu tun.
Ubuntu 14.04 python 3.5.1
Installieren Sie zuerst den Python-Wrapper mit pip.
pip install iperf3
Es ist auch möglich, direkt aus dem Github-Repository zu installieren, ohne pip zu verwenden.
git clone https://github.com/thiezn/iperf3-python.git
cd iperf3-python
python3 setup.py install
Serverseitiger 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('')
Dann der clientseitige 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))
Damit kann vorerst der Durchsatz des Netzwerks gemessen werden. Der Server (192.168.1.1) und der Client führen 10 Sekunden lang Messungen mit iperf3 durch. Obwohl in diesem Code nicht geändert, ist es möglich, die Portnummer und UDP / TCP sowie iperf anzugeben, die in der Befehlszeile ausgeführt werden.
https://pypi.python.org/pypi/iperf3 https://github.com/thiezn/iperf3-python
Recommended Posts