python - 如何使用 python 脚本从 eth0 获取带宽?

标签 python ubuntu module bandwidth

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/

相关文章:

python - 如何在 Python 3 中将二进制转换为字节再转换为十六进制?

python - 将元素分配给缓冲区和缓冲区中的特定位置 - 如何加速/为什么不同的实现如此慢?

python - 在ubuntu上导入caffe时ImportError cannot import name BytesIO

python-3.x - 无法获取缺少的 termios 模块。我如何正确获得它?

php - 构建可伸缩(可扩展)、可维护和松耦合软件的最佳技术是什么?

javascript - 模块 Node 浏览器错误

python - 为 pandas hist plots 集合添加标题

python - 如何知道 astype ('category' .cat.codes 分配的标签?

shell - 有没有办法在 linux 的 conf 文件中将文本添加到字符串的末尾?

ubuntu - 如果 RabbitMQ 停止,请保留该过程。这是否正常?