zoukankan      html  css  js  c++  java
  • psutil模块

    一:介绍

      psutil是一个用于检索信息的跨平台库(支持Linux/UNIX/OSX/Windows等),是运维期间经常使用的库,能查看系统利用率(CPU、内存、磁盘、网络),系统运行状态等

       安装:pip install psutil  或者进入https://pypi.org/project/psutil/下载安装

    二:一些实例  2.1获取进程相关信息Process

    import  psutil
    
    p=psutil.Process()
    print('进程号:{0}'.format(p.pid))
    print('进程名:{0}'.format(p.name()))
    print('进程的bin路径:{0}'.format(p.exe()))
    print('进程的工作目录绝对路径:{0}'.format(p.cwd()))
    print('进程状态:{0}'.format(p.status()))
    print('进程创建时间:{0}'.format(p.create_time()))
    print('进程的cpu时间信息:{0}'.format(p.cpu_times()))
    print('进程内存利用率:{0}'.format(p.memory_percent()))
    print('此进程的用户名:{0}'.format(p.username()))
    print('此进程是否在运行:{0}'.format(p.is_running()))
    print('此进程的父进程:{0}'.format(p.parent()))
    Process

    运行结果:

      2.2获取CPU信息

     

    import  psutil
    print('CPU逻辑数量:{0}'.format(psutil.cpu_count()))
    print('CPU物理核心:{0}'.format(psutil.cpu_count(logical=False)))
    print('当前全系统CPU的使用率:{0}'.format(psutil.cpu_percent(interval=1, percpu=True)))
    print('获取cpu的完整信息::{0}'.format(psutil.cpu_times()))
    cpu

     

    运行结果:

     

      2.3获取内存信息

    print('物理内存(返回的是字节为单位的整数):{0}'.format(psutil.virtual_memory()))
    print('交换内存的信息(返回的是字节为单位的整数):{0}'.format(psutil.swap_memory()))
    memory

    运行结果:

      2.4磁盘信息

    print('磁盘分区信息:{0}'.format(psutil.disk_partitions()))
    print('磁盘使用情况:{0}'.format(psutil.disk_usage('I:')))
    print('磁盘IO:{0}'.format(psutil.disk_io_counters()))
    disk

    运行结果:

      2.5网络信息

    import  psutil
    
    
    print('获取网络读写字节/包的个数:{0}'.format(psutil.net_io_counters()))
    # net_io_counters方法,如果需要获取单个网卡的io信息,加上pernic=True参数
    print('获取网络读写字节/包的个数:{0}'.format(psutil.net_io_counters(pernic=True)))
    print('获取网络接口信息:{0}'.format(psutil.net_if_addrs()))
    print('获取网络接口状态:{0}'.format(psutil.net_if_stats()))
    print('获取当前网络连接信息:{0}'.format(psutil.net_connections()))
    net

    运行结果:

      2.6其他信息

    import  psutil
    print('获取开机时间:{0}'.format(psutil.boot_time()))
    print('获取当前连接在系统上的用户:{0}'.format(psutil.users()))
    print('获取全部进程号:{0}'.format(psutil.pids()))
    
    print('类似于linux中的top效果:{0}'.format(psutil.test()))
    others

    运行结果: 

    三.实时获取网速小案例

     

    import psutil
    import time
    
    while True:
        s1 = psutil.net_io_counters(pernic=True)['以太网']
        time.sleep(1)
        s2 = psutil.net_io_counters(pernic=True)['以太网']
        result = s2.bytes_recv - s1.bytes_recv
        print(str(result / 1024) + 'kb/s')
    View Code

     

    运行结果:

     

     

    更多的请参阅源代码

    reference:psutil官网

     

    每天进步一点,遇见更好的你!
  • 相关阅读:
    实现Echarts折线图的虚实转换
    解决Webpack 安装sass时出现的错误
    Videojs视频插件在React中的应用
    overflow应用场景
    【JS Note】字符串截取
    【JS Note】undefined与null
    Web 网页常见问题集锦
    微信内置浏览器中,点击下拉框出现页面乱跳转现象(iphone)
    input美化 checkbox和radio样式
    Discuz!图片查看插件(支持鼠标缩放、实际大小、旋转、下载)
  • 原文地址:https://www.cnblogs.com/ganiner/p/9975606.html
Copyright © 2011-2022 走看看