4533 发表于 2015-12-24 09:33:27

python获取window下网卡流量的函数

目前只是获取window下网卡的流量信息,算是一个下载应用中的个性化插件效果体验。又需要的可以自己改进。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# coding:utf-8
__author__ = 'chenhuachao'
# --------------------------------
# Created by chenhuachaoon 2015/12/23.
# ---------------------------------
import wmi
import time
import platform

def get_network_flow(os):
    '''监控window平台下网卡的实时的流量信息
    通过当前总流量和一秒后的总流量的差值,来统计实时的网卡流量信息;
    返回的流量单位是KB
    '''
    if os == "Windows":
      c = wmi.WMI()
      for interfacePerTcp in c.Win32_PerfRawData_Tcpip_TCPv4():
            sentflow = float(interfacePerTcp.SegmentsSentPersec)#已发送的流量
            receivedflow = float(interfacePerTcp.SegmentsReceivedPersec) #接收的流量
            present_flow = sentflow+receivedflow    #算出当前的总流量
      time.sleep(1)
      for interfacePerTcp in c.Win32_PerfRawData_Tcpip_TCPv4():
         sentflow = float(interfacePerTcp.SegmentsSentPersec)#已发送的流量
         receivedflow = float(interfacePerTcp.SegmentsReceivedPersec) #接收的流量
         per_last_present_flow = sentflow+receivedflow   #算出1秒后当前的总流量
      present_network_flow = (per_last_present_flow - present_flow)/1024
      print "当前流量为:{0}KB".format("%.2f"%present_network_flow)
      return "%.2f"%present_network_flow

if __name__ =="__main__":
    os = platform.system()
    while 1:
      flow = get_network_flow(os)
      print "{0}KB".format(flow)






页: [1]
查看完整版本: python获取window下网卡流量的函数