python3 获取CPU、网卡、内存信息

python6年前 (2020)发布 admin
373 0

#_*_coding=utf-8 _*
#__author__ = 'chubby_superman'


import psutil
import datetime
import time
print('-----------------------------系统基本信息-----------------------------------')
# 当前时间
now_time = time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime(time.time()))
print(now_time)
# 系统启动时间
print(u"系统启动时间: %s" % datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))
# 系统用户
users_count = len(psutil.users())
users_list = ",".join([u.name for u in psutil.users()])
print(u"当前有%s个用户,分别是 %s" % (users_count, users_list))
print('-----------------------------cpu信息---------------------------------------')
# 查看cpu物理个数的信息
print(u"物理CPU个数: %s" % psutil.cpu_count(logical=False))
print(u"CPU核心总数: %s" % psutil.cpu_count())
#CPU的使用率
cpu = (str(psutil.cpu_percent(1))) + '%'
print(u"cup使用率: %s" % cpu)
print('-----------------------------mem信息---------------------------------------')
#查看内存信息,剩余内存.free  总共.total
#round()函数方法为返回浮点数x的四舍五入值。
free = str(round(psutil.virtual_memory().free / (1024.0 * 1024.0 * 1024.0), 2))
total = str(round(psutil.virtual_memory().total / (1024.0 * 1024.0 * 1024.0), 2))
memory = int(psutil.virtual_memory().total - psutil.virtual_memory().free) / float(psutil.virtual_memory().total)
print(u"物理内存: %s G" % total)
print(u"剩余物理内存: %s G" % free)
print(u"物理内存使用率: %s %%" % int(memory * 100))
print('-----------------------------网络信息---------------------------------------')
#网卡,可以得到网卡属性,连接数,当前流量等信息
net = psutil.net_io_counters()
bytes_sent = '{0:.2f} Mb'.format(net.bytes_recv / 1024 / 1024)
bytes_rcvd = '{0:.2f} Mb'.format(net.bytes_sent / 1024 / 1024)
print(u"网卡接收流量 %s 网卡发送流量 %s" % (bytes_rcvd, bytes_sent))

print('-----------------------------磁盘信息---------------------------------------')
io = psutil.disk_partitions()
#print("系统磁盘信息:" + str(io))

for i in io:
    try:
        o = psutil.disk_usage(i.device)
        ioo=psutil.disk_io_counters()
       # print(ioo)
    except Exception as e:
        pass
    print("%s盘总容量:"%i.device + str(int(o.total / (1024.0 * 1024.0 * 1024.0))) + "G,已用容量:" + str(int(o.used / (1024.0 * 1024.0 * 1024.0))) + "G,可用容量:" + str(int(o.free / (1024.0 * 1024.0 * 1024.0))) + "G")
    

网卡实时信息

# coding:utf-8
__author__ = 'chenhuachao'
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))

#每10秒钟执行一次
import threading as thd
import time

def fn():
    print(time.time())
    thd.Timer(10,fn).start()
    
fn()

#合成

import threading as thd
import psutil
import datetime
import time


def fn():
    print('-----------------------------系统基本信息-----------------------------------')
    # 当前时间
    now_time = time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime(time.time()))
    print(now_time)
    # 系统启动时间
    print(u"系统启动时间: %s" % datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))
    # 系统用户
    users_count = len(psutil.users())
    users_list = ",".join([u.name for u in psutil.users()])
    print(u"当前有%s个用户,分别是 %s" % (users_count, users_list))
    print('-----------------------------cpu信息---------------------------------------')
    # 查看cpu物理个数的信息
    print(u"物理CPU个数: %s" % psutil.cpu_count(logical=False))
    print(u"CPU核心总数: %s" % psutil.cpu_count())
    #CPU的使用率每10秒钟统计一次
    cpu = (str(psutil.cpu_percent(1))) + '%'
    print(u"cup使用率: %s" % cpu)
    print('-----------------------------mem信息---------------------------------------')
    #查看内存信息,剩余内存.free  总共.total
    #round()函数方法为返回浮点数x的四舍五入值。
    #每10秒钟统计一次
    free = str(round(psutil.virtual_memory().free / (1024.0 * 1024.0 * 1024.0), 2))
    total = str(round(psutil.virtual_memory().total / (1024.0 * 1024.0 * 1024.0), 2))
    memory = int(psutil.virtual_memory().total - psutil.virtual_memory().free) / float(psutil.virtual_memory().total)
    print(u"物理内存: %s G" % total)
    print(u"剩余物理内存: %s G" % free)
    print(u"物理内存使用率: %s %%" % int(memory * 100))
    print('-----------------------------网络信息---------------------------------------')
    #网卡,可以得到网卡属性,连接数,当前流量等信息
    #网卡信息每分钟统计一次
    net = psutil.net_io_counters()
    bytes_sent = '{0:.2f} Mb'.format(net.bytes_recv / 1024 / 1024)
    bytes_rcvd = '{0:.2f} Mb'.format(net.bytes_sent / 1024 / 1024)
    print(u"网卡接收流量 %s 网卡发送流量 %s" % (bytes_rcvd, bytes_sent))
    thd.Timer(10,fn).start()
    
fn()

© 版权声明

相关文章