Measure Network Data with Python

醉酒当歌 提交于 2019-11-29 12:43:53

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/8958614/measure-network-data-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!