用 Python 测量网络数据 [英] Measure Network Data with Python

查看:47
本文介绍了用 Python 测量网络数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个程序,当一段时间(比如半小时)网络流量低于某个阈值时,关闭计算机.

这是我使用过的伪代码将给出正确的逻辑:

开始子程序loopFlag = 真等待 5 分钟 # 留出时间引导和让机器稳定下来流量 = 0WHILE loopFlag = True 做FOR sec = 0 到 3600交通 += *network.traffic()*等待 1 秒ENDFOR如果流量<流量阈值 THEN循环标志 = 假万一结束时os.ShutDown()结束子程序

我正在寻找的是可以让我对此进行衡量的 Python 模块或库.

虽然我已经完成 各种 对此进行研究,这些似乎不是我所追求的那种功能,无论它们的语言如何.

关于如何实现这一点的任何想法?

解决方案

要检查系统上的网络流量,我建议您查看 psutil:

<预><代码>>>>psutil.net_io_counters(pernic=True){'lo':iostat(bytes_sent=799953745,bytes_recv=799953745,packets_sent=453698,packets_recv=453698),'eth0':iostat(bytes_sent=734324837,bytes_recv=4163935363,packets_sent=3605828,packets_recv=4096685)}>>>

并关闭你的操作系统,如果你在 Windows 上检查这个:操作系统重启、关机、休眠、睡眠、唤醒(Windows Python)

如果您使用的是 linux/unix,请使用 subprocess 模块发送 shutdown/<代码>重启命令.

I'm currently writing a program to shut down a computer when over a period of time (say, half an hour) network traffic is below a certain threshold.

Here's the pseudocode that I've worked will give the correct logic:

BEGIN SUBPROGRAM
    loopFlag = True
    Wait 5 minutes    # Allows time for boot and for the machine to settle
    traffic = 0
    WHILE loopFlag = True DO
        FOR sec = 0 to 3600
            traffic += *network.traffic()*
            wait 1 second
        ENDFOR
        IF traffic < trafficThreshold THEN
            loopFlag = False
        ENDIF
    ENDWHILE
    os.ShutDown()
END SUBPROGRAM

What I'm looking for is the Python module or library that will allow me to measure this.

Whilst I've done various research into this, these don't seem to be the sort of functionality I'm after, regardless of their language.

Any ideas on how to implement this?

解决方案

To check the network traffic on your system, i recommend you look into psutil:

>>> psutil.net_io_counters(pernic=True)
{'lo': iostat(bytes_sent=799953745, bytes_recv=799953745, packets_sent=453698, packets_recv=453698), 
 'eth0': iostat(bytes_sent=734324837, bytes_recv=4163935363, packets_sent=3605828, packets_recv=4096685)}
>>>

And to shutdown your OS, if you are on windows check this: OS Reboot, Shutdown, Hibernate, Sleep, Wakeup (Windows Python)

and if you are using linux/unix, use the subprocess module to send the shutdown/reboot command.

这篇关于用 Python 测量网络数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆