python 中的当前传入 MBPS
Current incoming MBPS in python
对于我的一个项目,我需要在 python 编码的 linux 上检测当前每秒传入的兆字节数。
我发现其他人的代码可以做正确的事情,但它的编码是 java,我不完全理解它。有什么指点吗?
TOTAL_INCOMING_BYTES_FILE = "/sys/class/net/%s/statistics/rx_bytes",
final double current_mbps =
((current_total_bytes - Long.parseLong(Files.readAllLines(Paths.get(new File(String.format(TOTAL_INCOMING_BYTES_FILE, ETH_INTERFACE)).toURI())).get(0))) / 125000) * (-1);
i found someone elses code that does the correct thing but its coded in java and i dont exactly understand it.
确实 Long.parseLong(Files.readAllLines(Paths.get(new File(String.format(TOTAL_INCOMING_BYTES_FILE, ETH_INTERFACE)).toURI())).get(0))
是一种从文件中读取数字的令人难以置信的复杂方法,更深奥一点的是,差的分数乘以 −1,而不是交换减数和被减数.
使用常量 125000,如果 current_total_bytes
已在八分之一秒前获取,则表达式计算 current_mbps
(因为 125000 是百万分之一)。代码中缺少 current_total_bytes
的(重新)初始化。
这是计算 e 的 Python 片段。 G。 eth0
率:
import time
earlier_total_bytes = None
while 0 <= (current_total_bytes := int(open("/sys/class/net/eth0/statistics/rx_bytes").read())) \
and earlier_total_bytes is None: earlier_total_bytes = current_total_bytes; time.sleep(.125)
current_mbps = (current_total_bytes - earlier_total_bytes) / 125000
当然这可以适用于其他采样间隔,也可以重复和可变。
对于我的一个项目,我需要在 python 编码的 linux 上检测当前每秒传入的兆字节数。
我发现其他人的代码可以做正确的事情,但它的编码是 java,我不完全理解它。有什么指点吗?
TOTAL_INCOMING_BYTES_FILE = "/sys/class/net/%s/statistics/rx_bytes",
final double current_mbps =
((current_total_bytes - Long.parseLong(Files.readAllLines(Paths.get(new File(String.format(TOTAL_INCOMING_BYTES_FILE, ETH_INTERFACE)).toURI())).get(0))) / 125000) * (-1);
i found someone elses code that does the correct thing but its coded in java and i dont exactly understand it.
确实 Long.parseLong(Files.readAllLines(Paths.get(new File(String.format(TOTAL_INCOMING_BYTES_FILE, ETH_INTERFACE)).toURI())).get(0))
是一种从文件中读取数字的令人难以置信的复杂方法,更深奥一点的是,差的分数乘以 −1,而不是交换减数和被减数.
使用常量 125000,如果 current_total_bytes
已在八分之一秒前获取,则表达式计算 current_mbps
(因为 125000 是百万分之一)。代码中缺少 current_total_bytes
的(重新)初始化。
这是计算 e 的 Python 片段。 G。 eth0
率:
import time
earlier_total_bytes = None
while 0 <= (current_total_bytes := int(open("/sys/class/net/eth0/statistics/rx_bytes").read())) \
and earlier_total_bytes is None: earlier_total_bytes = current_total_bytes; time.sleep(.125)
current_mbps = (current_total_bytes - earlier_total_bytes) / 125000
当然这可以适用于其他采样间隔,也可以重复和可变。