ubuntu ifconfig 中的 Python:如何从设备 eth0 获取带宽值在 python 脚本中使用 os 模块 ifconfig,如下所示:
$python script.py eth0
UP: 5 KB/sec
DOWN: 30.5 KB/sec
其中输出脚本可以每秒更改一次,值带宽使用 Kb/s ?
最佳答案
您可以使用 Psutil像这样获取有关网络接口(interface)的信息:
psutil.net_io_counters(pernic=True)
这将返回如下内容
{'awdl0': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0),
'bridge0': snetio(bytes_sent=342L, bytes_recv=0L, packets_sent=1L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0),
'en0': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0),
'en1': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0),
'en4': snetio(bytes_sent=68008896L, bytes_recv=1972984495L, packets_sent=776722L, packets_recv=1487084L, errin=0L, errout=10L, dropin=0L, dropout=0),
'lo0': snetio(bytes_sent=87119711L, bytes_recv=87119711L, packets_sent=54606L, packets_recv=54606L, errin=0L, errout=0L, dropin=0L, dropout=0)}
您可以每秒测量一次发送/接收字节的差异并打印上行/下行速度。如果您希望速度是人类可读的,请查看您如何使用 this .
关于python - 如何使用 python 脚本从 eth0 获取带宽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39494766/