python psutil 汇总 tcy

概要:
1.第一部分讲述基本语法
2.第二部分例程
  1)父子进程的判断。
  2)模块方法函数应用实例。
测试平台:win7 python3.7
内容有点多,主要讲述psutil.Process,其他简介;请仔细看
psutil  模块   2018/9/23
import psutil
---------------------------------------------------------------------------------------------
1.1.描述
psutil是一个用于检索信息的跨平台库
运行进程和系统利用率(CPU,内存,磁盘,网络,传感器)在Python中。
1.2.模块内容
    psutil.Process
    psutil.test()
    psutil.Popen
    psutil.AccessDenied
ZombieProcess类(NoSuchProcess)
---------------------------------------------------------------------------------------------
2.class Popen(Process)
    用途:
    # stdlib subprocess.Popen类的更方便的接口。
    # 它启动一个子流程并完全像使用时一样处理它subprocess.Popen类,提供了所有psutil.Process类的属性和方法
    说明:
    # 与subprocess.Popen不同,此类先发制人地检查是否为PID已经在send_signal(),terminate()和kill()上重用了这个
    # 你不会意外地终止另一个进程,修复http://bugs.python.org/issue6973。
    # 
    # 有关完整文档,请参阅:
    # http://docs.python.org/library/subprocess.html
    方法:
    wait(self, timeout=None)
        # 等待进程终止, 如果进程是子进程os.getpid(), 也返回其退出代码, 否则为None。
        # 要等待多个进程, 请使用psutil.wait_procs()。
    实例:
    import psutil
    from subprocess import PIPE
    
    p = psutil.Popen(["python", "-c", "print( 'hi')"], stdout=PIPE)
    a=p.name()
    print(a)
    # 'python'
    # a1=psutil.Process.uids
    # print(a)
    # user(real=1000, effective=1000, saved=1000)
    a=p.username()
    
    print(a)
    # 'giampaolo'
    a=p.communicate()
    print(p.communicate())
    # ('hi', None)
    p.terminate()
    p.wait(timeout=2)
  
-------------------------------------------------- ------------------------------
3.class Process
class  Process(pid=None) 表示具有给定PID的OS进程。
    参数:
        # 如果省略PID,则使用当前进程PID(os.getpid())
        # 如果PID不存在,则提升NoSuchProcess。
    # 注意
    #     进程标识的唯一例外是先发制人检查和保证是:
    #     - parent()
    #     - children()
    #     - nice() (set)
    #     - ionice() (set)
    #     - rlimit() (set)
    #     - cpu_affinity (set)
    #     - suspend()暂停
    #     - resume()恢复
    #     - send_signal()
    #     - terminate()终止
    #     - kill()
    #
    # 解决办法:
    # - 在查询进程之前使用is_running()
    # - 如果你不断迭代一组进程实例使用进行先发制人检查的process_iter()每个产生的实例的进程标识
    属性:
        pid     #process PID.

---------------------------------------------------------------------------------
3.2.方法:
as_dict(self,attrs = None,ad_value = None)实用方法返回流程信息可以翻译的字典。
    # attrs 必须是字符串列表反映可用Process类的属性名称如['cpu_times','name']否则全部公开(读only)假设属性。
    # ad_value 是在案例中分配的值当引发AccessDenied或ZombieProcess异常时检索特定的流程信息。

    children(self, recursive=False)将此过程的子项作为Process列表返回实例,先发制人地检查PID是否已被重用。
        # recursive=True,则返回所有父后代。
        Example (A == this process):

        A ─┐
           │
           ├─ B (child) ─┐
           │             └─ X (grandchild) ─┐
           │                                └─ Y (great grandchild)
           ├─ C (child)
           └─ D (child)

        >>> p = psutil.Process()
        >>> p.children()
        # B,C,D
        >>> p.children(recursive = True)
        # B,X,Y,C,D

    cmdline(self)已调用此进程的命令行。
    #
    connections(self, kind='inet')返回由进程打开的套接字连接作为列表
        # (fd,family,type,laddr,raddr,status)namedtuples。
        # * kind *参数筛选匹配的连接

    cpu_affinity(self,cpus = None)获取或设置进程CPU关联。
        # 如果指定,* cpus *必须是您的CPU列表想要设置亲和力(例如[0,1])。
        # 如果传递空列表,则假定所有可用的CPU(并设定)。(仅限Windows,Linux和BSD)。
    #
    cpu_percent(self,interval = None)返回表示当前进程CPU的浮点数利用率百分比。
    #
    cpu_times(self)
        # 返回a(user,system,children_user,children_system)
        # 表示累计处理时间的namedtuple秒。这类似于os.times()但是每个进程。
        # 在macOS和Windows上,children_user和children_system是始终设为0。
    #
    create_time(self)进程创建时间为浮点数自UTC以来以秒为单位表示。
        # 首次调用后会返回返回值。
    #
    cwd(self)将当前工作目录作为绝对路径处理。
    #
    environ(self)作为dict的进程的环境变量。
    #
    exe(self)该进程可作为绝对路径执行。
        # 也可能是一个空字符串。首次调用后会返回返回值。
    #
    io_counters(self)返回进程I / O统计信息
        # 为(read_count,write_count,read_bytes,write_bytes)namedtuple。
        # 这些是执行的读/写调用次数和进程读取和写入的字节数。
    #
    ionice(self,ioclass = None,value = None)获取或设置进程I / O良好性(优先级)。
        # 在Linux *上,ioclass *是IOPRIO_CLASS_ *常量之一。
        # * value *是一个从0到7的数字value,进程的I / O优先级越低。
        # 在Windows上仅使用* ioclass *,它可以设置为2(正常),1(低)或0(非常低)。仅适用于Vista。
    #
    is_running(self)返回此进程是否正在运行。
        # 它还检查PID是否已被其他进程重用哪个案例返回False。
    #
    memory_full_info(self)此方法返回与memory_info()相同的信息
    #
    memory_info(self)返回一个带有可变字段的namedtuple,取决于平台,表示有关进程的内存信息。
#
    memory_maps(self,grouped = True) 返回进程'将内存区域映射为namedtuples列表
        # 其字段可变,具体取决于平台。

    memory_percent(self,memtype ='rss')将进程内存与总物理系统内存进行比较
        # 以百分比计算进程内存利用率。
        # * memtype *参数是一个字符串,指示什么类型的要比较的进程内存(默认为“rss”)。
        # 可以像这样获得可用字符串列表:
        >>> psutil.Process().memory_info()._fields
        # ('rss','vms','shared','text','lib','data','dirty','uss','pss')

    name(self)进程名称。首次调用后会返回返回值。

    nice(self, value=None)获取或设置进程的好处(优先级)。

    num_ctx_switches(self)返回自愿和非自愿背景的数量由此过程执行的开关。

    num_handles(self)返回此进程打开的句柄数(仅限Windows)。

    num_threads(self)返回此进程使用的线程数。

    oneshot(self)实用程序上下文管理器,大大加快了同时检索多个过程信息。
        >>> p = psutil.Process()
        >>> with p.oneshot():
        ... p.name()#收集多个信息
        ... p.cpu_times()#返回缓存值
        ... p.cpu_percent()#返回缓存值
        ... p.create_time()#返回缓存值

    open_files(self)将进程打开的文件作为列表返回
        # (path,fd)namedtuples包括绝对文件名和文件描述符号。

    parent(self)先发制人地将父进程作为Process对象返回检查PID是否已被重用。
        # 如果没有父母知道,则返回None。

    ppid(self)进程父PID。在Windows上,首次调用后会返回返回值。

    resume(self)使用SIGCONT进行先发制人检查以恢复流程执行PID是否已被重用。
        # 在Windows上,这具有恢复所有进程线程的效果。

    send_signal(self,sig)发送信号* sig *以进行先发制人检查
        # PID是否已被重用(参见信号模块常量)。在Windows上,只有SIGTERM有效并被视为别名

    status(self)过程当前状态为STATUS_ *常量。

    suspend(self)使用SIGSTOP暂停检查暂停流程执行PID是否已被重用。
        # 在Windows上,这具有暂停所有进程线程的效果。

    terminate(self)使用SIGTERM进行先发制人检查以终止该过程
        # PID是否已被重用。在Windows上,这是kill()的别名。

    threads(self)返回由进程打开的线程作为列表
        # (id,user_time,system_time)namedtuples表示线程ID和线程CPU时间(用户/系统)。

    username(self)拥有该进程的用户的名称。

    wait(self, timeout=None)等待进程终止,如果进程是子进程os.getpid(),也返回其退出代码,否则为None。
        # 如果进程已经终止,则立即返回None而不是提高NoSuchProcess。
        # 如果指定了* timeout *(以秒为单位)并且进程仍然存在live raise TimeoutExpired。
        # 要等待多个进程,请使用psutil.wait_procs()。

--------------------------------------------------------- --------------------
ZombieProcess(pid,name = None,ppid = None,msg = None)查询僵尸进程时出现异常。
    # 这是仅在macOS,BSD和Solaris上提出,并不总是:依赖在查询上,OS无论如何都可能成功。
    # 在Linux上,所有僵尸进程都是可以使用的(因此这绝不是上调)。 Windows没有僵尸进程。
'''''''''
--------------------------------------------------------- --------------------
3.FUNCTIONS
    boot_time()返回自纪元以来以秒表示的系统启动时间。
    cpu_count(logical=True)系统逻辑CPU的数量os.cpu_count()
    cpu_percent(interval = None,percpu = False)返回当前系统CPU浮点数利用率百分比。
        # interval > 0.0时,比较之前经过的系统CPU时间间隔后(阻塞)。
        # interval=0.0或None时,第一次被调用返回0.0值,你应该忽略它。延时0.1秒
        # percpu=True时,返回表示代表的浮点数列表利用率作为每个CPU的百分比。
    例子:
        psutil.cpu_percent(interval=1)#2
        psutil.cpu_percent(interval=1, percpu=True)#[2.0, 1.0]
        psutil.cpu_percent(interval=None)#2.9
          

    cpu_stats()返回CPU统计信息。

    cpu_times(percpu=False)将系统范围的CPU时间作为命名元组返回。
        # 每个CPU时间代表CPU花费的秒数
        # 给定模式。 namedtuple的字段可用性取决于平台:
        #  - user
        #  - system
        #  - idle
        #  - nice (UNIX)
        #  - iowait (Linux)
        #  - irq (Linux, FreeBSD)
        #  - softirq (Linux)
        #  - steal (Linux >= 2.6.11)
        #  - guest (Linux >= 2.6.24)
        #  - guest_nice (Linux >= 3.2.0)
        #
        # percpu =True时,返回每个CPU的namedtuples列表。

    cpu_times_percent(interval = None,percpu = False)
        # 与cpu_percent()相同,但提供利用率百分比对于cpu_times()返回的每个特定CPU时间。
        例如:
        cpu_times_percent()
            # cpupercent(user = 4.8,nice = 0.0,system = 4.8,idle = 90.5,iowait = 0.0,
            #  irq = 0.0,softirq = 0.0,steal = 0.0,guest = 0.0,guest_nice = 0.0)

    disk_io_counters(perdisk = False,nowrap = True)将系统磁盘I / O统计信息作为包含的命名元组返回
        # 字段:
        # -  read_count:读取次数
        # -  write_count:写入次数
        # -  read_bytes:读取的字节数
        # -  write_bytes:写入的字节数
        # -  read_time:从磁盘读取的时间(以ms为单位)
        # -  write_time:写入磁盘所花费的时间(以ms为单位)
        #
        # 特定平台:
        # -  busy_time :( Linux,FreeBSD)花在实际I / O上的时间(以ms为单位)
        # -  read_merged_count(Linux):合并读取的数量
        # -  write_merged_count(Linux):合并写入的数量

    disk_partitions(all=False)将安装的分区作为列表返回
        # (device,mountpoint,fstype,opts)namedtuple。
        # 'opts'字段是一个由逗号分隔的原始字符串,表示mount

    disk_usage(path)返回有关给定* path *的磁盘使用情况统计信息
        # namedtuple包括以字节为单位表示的total,used和free空间加上使用百分比。

    net_connections(kind='inet')将系统范围的套接字连接作为列表返回
        # (fd,family,type,laddr,raddr,status,pid)namedtuples。
        # kind 参数过滤适合的连接
        #     inet  IPv4和IPv6
        #     inet4  IPv4
        #     inet6  IPv6
        #     tcp  TCP
        #     tcp4  TCP over IPv4
        #     tcp6  TCP over IPv6
        #     udp  UDP
        #     udp4  UDP over IPv4
        #     udp6  UDP over IPv6
        #     unix        UNIX socket (both UDP and TCP protocols)
        #     all         the sum of all the possible families and protocols

    net_if_addrs()
        # 返回与每个NIC关联的地址(网络接口卡)作为字典安装在系统上,其键是NIC名称和值是每个地址的命名元组列表
        # 分配给NIC。每个namedtuple包括5个字段:
        # -  family:可以是socket.AF_INET,socket.AF_INET6或psutil.AF_LINK,指的是MAC地址。
        # -  address:是主要地址,始终设置。
        # -  netmask:'broadcast'和'ptp'可以是None。
        # -  ptp:代表“点对点”并引用点对点接口上的目标地址(通常是VPN)。
        # -  broadcast:和* ptp *是互斥的。

    net_if_stats()返回有关每个NIC(网络接口卡)的信息
        # 作为字典安装在系统上,其键是NIC名称和值是一个带有以下字段的namedTuple:
        # -  isup:接口是否启动(bool)
        # -  duplex:可以是NIC_DUPLEX_FULL,NIC_DUPLEX_HALF或NIC_DUPLEX_UNKNOWN
        # - speed:NIC速度以兆位(MB)表示;如果不能确定(例如'localhost')它将被设置为0。
        # -  mtu:以字节表示的最大传输单位。

    net_io_counters(pernic = False,nowrap = True)将网络I / O统计信息作为包含的命名元组
        # 返回以下字段:
        # -  bytes_sent:发送的字节数
        # -  bytes_recv:接收的字节数
        # -  packets_sent:发送的包数
        # -  packets_recv:接收的数据包数
        # -  errin:接收时的错误总数
        # -  errout:发送时的错误总数
        # -  dropin:丢弃的传入数据包总数
        # -  dropout:丢弃的传出数据包总数

    pid_exists(pid)如果当前进程列表中存在给定的PID,则返回True。
        # 比在psutil.pids()中执行“pid”更快应该是首选。

    pids()返回当前运行的PID列表。

    process_iter(attrs =None,ad_value =None)返回一个生成器,为所有人生成一个Process实例运行进程。

    sensors_battery()返回电池信息。如果没有安装电池返回无。
        # -  percent:电池剩余电量百分比。
        # -  secsleft:剩余多少秒的粗略近似值在电池电量耗尽之前。也许POWER_TIME_UNLIMITED或POWER_TIME_UNLIMITED。
        # -  power_plugged:如果连接了交流电源线,则为真。

    swap_memory()将系统交换内存统计信息作为包含的命名元组
        # 返回以下字段:
        # -  total:总交换内存(以字节为单位)
        # -  used:使用的swap字节数
        # -  free:以字节为单位的免费交换内存
        # -  percent:使用百分比
        # -  sin:系统从磁盘交换的字节数(累计)
        # -  sout:系统从磁盘换出的字节数(累计)
        # Windows上的'sin'和'sout'毫无意义,总是设置为0。

    users()返回当前在系统上连接的用户列表
        # namedtuples包括以下字段。
        # -  user:用户的名称
        # -  terminal:与用户关联的tty或伪tty,如果有的话。
        # -  host:与条目关联的主机名(如果有)。
        # - start:创建时间表示为浮点数自纪元以来的几秒钟。

    virtual_memory()将有关系统内存使用情况的统计信息作为namedtuple
        # 返回包括以下字段,以字节表示:
        # - 总计:可用的总物理内存。
        # - 可用:
        # - 百分:百分比用法计算为(总计 - 可用)/总计* 100
        #
        # - 用过的:
        # - 自由:
        # 特定于平台的字段:
        # - 活动(UNIX):目前正在使用或最近使用的内存,因此它在RAM中。
        # - 不活动(UNIX):标记为未使用的内存。
        # - 缓冲区(BSD,Linux):缓存文件系统元数据之类的东西。
        # - 缓存(BSD,macOS):缓存各种事物。
        # - 有线(macOS,BSD):标记为始终保留在RAM中的内存。它永远不会移动到磁盘。
        # - 共享(BSD):可以由多个进程同时访问的内存。
        #
        # 'used'和'available'的总和不一定等于total。
        # 在Windows上''available'和'free'是一样的。

    wait_procs(procs,timeout = None,callback = None)等待进程列表的便捷功能终止。
        # 返回一个(消失的,活的)元组,指示哪些进程已经不见了,哪些还活着。
        # * callback *是一个每次进程都会被调用的函数终止(Process实例作为回调参数传递)。
        # 一旦所有进程终止或何时,函数将立即返回*超时*发生。与Process.wait()不同,它不会引发TimeoutExpired*超时*发生。
        典型用例:
            - 将SIGTERM发送到进程列表
            - 给他们一些时间来终止
            - 将SIGKILL发送给那些还活着的人

            例:

            >>> def on_terminate(proc):
            ... print(“process {} terminate”.format(proc))
            ...
            >>> for proc in procs:
            ... p.terminate()
            ...
            >>> gone,alive = wait_procs(procs,timeout = 3,callback = on_terminate)
            >>> for p in alive:
            ... p.kill()

win_service_get(名称)通过* name *获取Windows服务。
    # 如果不存在具有此类名称的服务,则引发NoSuchProcess。

win_service_iter()返回一个生成器,为所有人生成一个WindowsService实例已安装Windows服务。
    数据
    ABOVE_NORMAL_PRIORITY_CLASS = 32768
    AF_LINK = <AddressFamily.AF_LINK: -1>
    AIX = False
    BELOW_NORMAL_PRIORITY_CLASS = 16384
    BSD = False
    CONN_CLOSE = 'CLOSE'
    CONN_CLOSE_WAIT = 'CLOSE_WAIT'
    CONN_CLOSING = 'CLOSING'
    CONN_DELETE_TCB = 'DELETE_TCB'
    CONN_ESTABLISHED = 'ESTABLISHED'
    CONN_FIN_WAIT1 = 'FIN_WAIT1'
    CONN_FIN_WAIT2 = 'FIN_WAIT2'
    CONN_LAST_ACK = 'LAST_ACK'
    CONN_LISTEN = 'LISTEN'
    CONN_NONE = 'NONE'
    CONN_SYN_RECV = 'SYN_RECV'
    CONN_SYN_SENT = 'SYN_SENT'
    CONN_TIME_WAIT = 'TIME_WAIT'
    FREEBSD = False
    HIGH_PRIORITY_CLASS = 128
    IDLE_PRIORITY_CLASS = 64
    LINUX = False
    MACOS = False
    NETBSD = False
    NIC_DUPLEX_FULL = <NicDuplex.NIC_DUPLEX_FULL: 2>
    NIC_DUPLEX_HALF = <NicDuplex.NIC_DUPLEX_HALF: 1>
    NIC_DUPLEX_UNKNOWN = <NicDuplex.NIC_DUPLEX_UNKNOWN: 0>
    NORMAL_PRIORITY_CLASS = 32
    OPENBSD = False
    OSX = False
    POSIX = False
    POWER_TIME_UNKNOWN = <BatteryTime.POWER_TIME_UNKNOWN: -1>
    POWER_TIME_UNLIMITED = <BatteryTime.POWER_TIME_UNLIMITED: -2>
    REALTIME_PRIORITY_CLASS = 256
    STATUS_DEAD = 'dead'
    STATUS_DISK_SLEEP = 'disk-sleep'
    STATUS_IDLE = 'idle'
    STATUS_LOCKED = 'locked'
    STATUS_PARKED = 'parked'
    STATUS_RUNNING = 'running'
    STATUS_SLEEPING = 'sleeping'
    STATUS_STOPPED = 'stopped'
    STATUS_TRACING_STOP = 'tracing-stop'
    STATUS_WAITING = 'waiting'
    STATUS_WAKING = 'waking'
    STATUS_ZOMBIE = 'zombie'
    SUNOS = False
    WINDOWS = True
----------------------------------------------------------------------------------
1.获取父子进程 2018/9/27
# ------------------------------------------------------------------------------
 
import time, psutil

start=time.time()

def run_fun(i):
global start
# time.sleep(1)
print(i,time.time()-start)

def chrome_process():
#1.获取系统进程pid,name
get_system_pids_names={i:psutil.Process(i).name() for i in psutil.pids()}
#2. 获取chrome所有进程pid,name
get_chrome_pids_names = {k: v for k, v in get_system_pids_names.items() if 'chrome' in v}
#3. 获取python当前进程pid,name
get_python_pid_name = {psutil.Process().name():psutil.Process().pid}
#4. 获取chrome子进程pid,name
get_chrome_child_pids_names = [
psutil.Process(i).children()[i1].pid
for i in get_chrome_pids_names.keys()
for i1 in range(len(psutil.Process(i).children()))
if psutil.Process(i).children()
]
#5. 获取chrome父进程pid,name
get_chrome_parent_pids_names ={
psutil.Process(i).parent().name():psutil.Process(i).parent().pid for i in get_chrome_pids_names.keys()}
#6. 获取chrome当前进程pid,name
get_chrome_pid_name = {'chrome.exe': get_chrome_parent_pids_names['chrome.exe']}
print('1.获取系统 进程pid,name', get_system_pids_names)
print('2.获取chrome所有进程pid,name', get_chrome_pids_names)
print('3.获取chrome当前进程pid,name', get_python_pid_name,get_chrome_pid_name)
print('4.获取chrome 子进程pid,name', get_chrome_child_pids_names)
print('5.获取chrome 父进程pid,name', get_chrome_parent_pids_names)
    
if __name__ == "__main__":
 chrome_process()
    '''
    显示结果:
    1.获取系统      进程pid,name {0: 'System Idle Process', 4: 'System', 476: 'smss.exe', 712: 'csrss.exe', 884: 'wininit.exe', 904: 'csrss.exe', 256: 'services.exe', 292: 'lsass.exe', 288: 'lsm.exe', 356: 'winlogon.exe', 736: 'svchost.exe', 944: 'DesktopService64.exe', 992: 'QQPCRTP.exe', 1128: 'svchost.exe', 1216: 'atiesrxx.exe', 1296: 'svchost.exe', 1352: 'svchost.exe', 1380: 'svchost.exe', 1512: 'audiodg.exe', 1560: 'svchost.exe', 1652: 'atieclxx.exe', 1752: '2345SafeSvc.exe', 1772: '2345SoftSvc.exe', 1832: 'Protect_2345Explorer.exe', 1896: 'svchost.exe', 1108: 'wlanext.exe', 1136: 'conhost.exe', 1424: 'dwm.exe', 2084: 'explorer.exe', 2236: 'taskeng.exe', 2244: 'spoolsv.exe', 2284: 'taskhost.exe', 2356: 'svchost.exe', 2424: 'dllhost.exe', 2592: 'almsrv64x.exe', 2648: 'OfficeClickToRun.exe', 2700: 'QQPCTray.exe', 2728: 'ALMPanelPlugin.exe', 2744: '2345SafeTray.exe', 2816: '2345RTProtect.exe', 2896: 'RAVCpl64.exe', 2136: 'DesktopMgr64.exe', 3152: 'HaoZipSvc.exe', 3444: 'mqsvc.exe', 3572: 'sqlservr.exe', 3468: 'sqlservr.exe', 4488: 's7oiehsx64.exe', 4576: 'S7TraceService64x.exe', 4688: 'svchost.exe', 4828: 'SearchIndexer.exe', 4912: 's7epasrv64x.exe', 5780: 'Shield_2345Explorer.exe', 5432: 'unsecapp.exe', 5552: 'WmiPrvSE.exe', 6776: 'svchost.exe', 7272: 'QyClient.exe', 3968: 'QyPlayer.exe', 4160: 'QyFragment.exe', 1944: 'AndroidService.exe', 7012: 'QyKernel.exe', 8300: 'WeCap.exe', 8484: 'svchost.exe', 6464: 'rundll32.exe', 5152: 'SogouCloud.exe', 7240: 'winchm.exe', 8628: 'chrome.exe', 1372: 'chrome.exe', 6792: 'chrome.exe', 6692: 'chrome.exe', 7364: 'chrome.exe', 8796: 'chrome.exe', 9336: 'chrome.exe', 9356: 'chrome.exe', 9376: 'chrome.exe', 9580: 'chrome.exe', 9596: 'chrome.exe', 9612: 'chrome.exe', 9628: 'chrome.exe', 9648: 'chrome.exe', 9660: 'chrome.exe', 9672: 'chrome.exe', 9684: 'chrome.exe', 9700: 'chrome.exe', 8700: 'pycharm64.exe', 10636: 'fsnotifier64.exe', 10664: 'conhost.exe', 11016: 'chrome.exe', 11112: 'WINWORD.EXE', 10276: 'chrome.exe', 10576: 'chrome.exe', 10764: 'pythonw.exe', 10984: 'pythonw.exe', 760: 'PowerWord.exe', 10592: 'powerwordhelper.exe', 10880: 'CBGrabConnect_x64.exe', 6428: 'taskeng.exe', 11156: 'ctfmon.exe', 4820: 'pythonw.exe', 8636: 'pythonw.exe', 11188: 'pythonw.exe', 4328: 'pythonw.exe', 2920: 'pythonw.exe', 11192: 'pwsearch.exe', 5816: 'Helper_2345Explorer.exe', 8272: 'SearchProtocolHost.exe', 3752: 'PyScripter.exe', 10860: 'python.exe', 6248: 'conhost.exe', 10416: 'SearchFilterHost.exe', 11236: 'python.exe', 9520: 'conhost.exe'}
    2.获取chrome所有进程pid,name {8628: 'chrome.exe', 1372: 'chrome.exe', 6792: 'chrome.exe', 6692: 'chrome.exe', 7364: 'chrome.exe', 8796: 'chrome.exe', 9336: 'chrome.exe', 9356: 'chrome.exe', 9376: 'chrome.exe', 9580: 'chrome.exe', 9596: 'chrome.exe', 9612: 'chrome.exe', 9628: 'chrome.exe', 9648: 'chrome.exe', 9660: 'chrome.exe', 9672: 'chrome.exe', 9684: 'chrome.exe', 9700: 'chrome.exe', 11016: 'chrome.exe', 10276: 'chrome.exe', 10576: 'chrome.exe'}
    3.获取chrome当前进程pid,name {'python.exe': 11236} {'chrome.exe': 8628}
    4.获取chrome  子进程pid,name [1372, 6792, 6692, 7364, 8796, 9336, 9356, 9376, 9580, 9596, 9612, 9628, 9648, 9660, 9672, 9684, 9700, 11016, 10276, 10576]
    5.获取chrome  父进程pid,name {'DesktopMgr64.exe': 2136, 'chrome.exe': 8628}
    
    注意:win7chrome平台上第一运行错误,再次运行正确;在其他平台上测试正常。

    '''
      

    
# ------------------------------------------------------------------------------
2.常见使用
 
 2.1 CPU

import psutil,time,os,sys

a1=psutil.cpu_count()               # 查看CPU逻辑个数 4
a2=psutil.cpu_count(logical=False)  # 查看CPU物理个数 4
# ++++++++++++++++++++++++++++++++++
time.sleep(0.1)                     #必须加延时
a3=psutil.cpu_percent()             #查看CPU使用率 42.9
a=psutil.cpu_percent(percpu=True)   # 查看每个CPU的使用情况 [8.7, 26.9, 28.4, 17.9]
#+++++++++++++++++++++++++++++++++++
# 查看CPU时间分配情况
a=psutil.cpu_times()
# scputimes(user=4313.6928517, system=3114.5131647000017,
#           idle=32382.983981899997, interrupt=121.992782, dpc=281.0046013)
# 查看CPU的工作频率
a=psutil.cpu_freq()
# scpufreq(current=3000.0, min=0.0, max=3000.0)

#-------------------------------------------------------------------------------
2.2内存
a=psutil.swap_memory()
# sswap(total=20934602752, used=5738786816, free=15195815936, percent=27.4, sin=0, sout=0)
a=psutil.virtual_memory()
# svmem(total=10468253696, available=5129367552, percent=51.0, used=5338886144, free=5129367552)
a=psutil.virtual_memory().total
# 10468253696

#-------------------------------------------------------------------------------
2.3磁盘
# 查看所有分区信息
a=psutil.disk_partitions()
# [sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed'),
# sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed'),
# sdiskpart(device='E:\\', mountpoint='E:\\', fstype='NTFS', opts='rw,fixed'),
#  sdiskpart(device='F:\\', mountpoint='F:\\', fstype='NTFS', opts='rw,fixed')]
#
# 查看C盘使用情况
a=psutil.disk_usage("c:\\")
# sdiskusage(total=120028393472, used=61686546432, free=58341847040, percent=51.4)
#
# 查看磁盘读写操作情况
a=psutil.disk_io_counters()
# sdiskio(read_count=303583, write_count=211606, read_bytes=7324027392,
#         write_bytes=4552508416, read_time=343, write_time=141)
#
a=psutil.disk_io_counters(perdisk=True)
#{'PhysicalDrive0': sdiskio(read_count=140090, write_count=1870,
  #read_bytes=3438887424,write_bytes=555118592, read_time=120, write_time=4),
# 'PhysicalDrive1': sdiskio(read_count=164110, write_count=210165,
#  read_bytes=3896839168, write_bytes=4032776704, read_time=224, write_time=139)}

# -----------------------------------------------------------------------------------
2.4网络
a=psutil.net_io_counters()
# snetio(bytes_sent=1433507511, bytes_recv=646545243, packets_sent=1811127,
# packets_recv=1906198, errin=0, errout=0, dropin=0, dropout=0)
a=psutil.net_io_counters(pernic=True)

# {'无线网络连接 2': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0,
#  errin=0, errout=0, dropin=0, dropout=0),
# '无线网络连接': snetio(bytes_sent=1437087578, bytes_recv=646884138, packets_sent=1815125,
#  packets_recv=1910240, errin=0, errout=0, dropin=0, dropout=0),
# '本地连接': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0,
#  errin=0, errout=0, dropin=0, dropout=0),
# 'Loopback Pseudo-Interface 1': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0,
#  packets_recv=0, errin=0, errout=0, dropin=0, dropout=0),
# 'isatap.{48C6D664-B85E-474D-B84C-5D5AA3ACA123}': snetio(bytes_sent=0, bytes_recv=0,
# packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0),
# 'Teredo Tunneling Pseudo-Interface': snetio(bytes_sent=0, bytes_recv=0,
# packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0),
# 'isatap.{6985F0D2-E187-4C05-8D88-166DBFA20A8B}': snetio(bytes_sent=0, bytes_recv=0,
# packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0),
# 'isatap.{2C673B69-0B9F-4635-92EB-A8BDC9A7F5CD}': snetio(bytes_sent=0, bytes_recv=0,
# packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}
#
a=psutil.net_connections()

# [sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=12331), raddr=(), status='NONE', pid=8460),
# sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='::', port=445), raddr=(), status='LISTEN', pid=4),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=49155), raddr=(), status='LISTEN', pid=1304),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=49153), raddr=(), status='LISTEN', pid=884),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50476),raddr=addr(ip='36.110.238.76', port=80), status='CLOSE_WAIT', pid=4472),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50623),raddr=addr(ip='101.227.199.28', port=443), status='CLOSE_WAIT', pid=6280),
# sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='::', port=2105), raddr=(), status='LISTEN', pid=2176),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=5355), raddr=(), status='NONE', pid=1872),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50579),raddr=addr(ip='58.215.115.164', port=443), status='CLOSE_WAIT', pid=6280),
# sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 2,laddr=addr(ip='::', port=4500), raddr=(), status='NONE', pid=1396),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='192.168.31.185', port=138), raddr=(), status='NONE', pid=4),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=445), raddr=(), status='LISTEN', pid=4),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=39390), raddr=(), status='NONE', pid=4472),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=52233), raddr=(), status='NONE', pid=2068),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50622),raddr=addr(ip='101.227.199.28', port=443), status='CLOSE_WAIT', pid=6280),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 50742),raddr=addr(ip='127.0.0.1', port=50743), status='ESTABLISHED', pid=4472),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 58129),raddr=addr(ip='203.208.40.100', port=80), status='TIME_WAIT', pid=0),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 50745),raddr=addr(ip='127.0.0.1', port=50744), status='ESTABLISHED', pid=4472),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=4500), raddr=(), status='NONE', pid=1396),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50333),raddr=addr(ip='36.110.238.76', port=80), status='CLOSE_WAIT', pid=4472),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 50735),raddr=addr(ip='127.0.0.1', port=50734), status='ESTABLISHED', pid=4472),
# sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='::', port=2107), raddr=(), status='LISTEN', pid=2176),
# sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='::', port=49204), raddr=(), status='LISTEN', pid=256),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=16423), raddr=(), status='LISTEN', pid=6280),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=7542), raddr=(), status='NONE', pid=4472),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port=6942), raddr=(), status='LISTEN', pid=4816),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port=1801), raddr=(), status='LISTEN', pid=2176),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=1370), raddr=(), status='LISTEN', pid=4472),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=6000), raddr=(), status='NONE', pid=8204),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=49207), raddr=(), status='LISTEN', pid=6732),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=43199), raddr=(), status='NONE', pid=8460), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='192.168.31.185', port=137), raddr=(), status='NONE', pid=4),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=49152), raddr=(), status='NONE', pid=992), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=2103), raddr=(), status='LISTEN', pid=2176),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=43198), raddr=(), status='NONE', pid=6632), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 50743),raddr=addr(ip='127.0.0.1', port=50742), status='ESTABLISHED', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='::', port=135), raddr=(), status='LISTEN', pid=1124), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='::', port=49172), raddr=(), status='LISTEN', pid=288), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 50740),raddr=addr(ip='127.0.0.1', port=50741), status='ESTABLISHED', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='::', port=2103), raddr=(), status='LISTEN', pid=2176), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='::', port=49354), raddr=(), status='LISTEN', pid=4092), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=56953), raddr=(), status='NONE', pid=7660), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50324),raddr=addr(ip='150.138.210.42', port=5333), status='ESTABLISHED', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 58146),raddr=addr(ip='203.208.50.55', port=80), status='TIME_WAIT', pid=0), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=27753), raddr=(), status='NONE', pid=6632), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port=139), raddr=(), status='LISTEN', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 53718),raddr=addr(ip='127.0.0.1', port=53717), status='ESTABLISHED', pid=6552), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 58170),raddr=addr(ip='36.110.238.94', port=80), status='TIME_WAIT', pid=0), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=49354), raddr=(), status='LISTEN', pid=4092), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 50737),raddr=addr(ip='127.0.0.1', port=50736), status='ESTABLISHED', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 50741),raddr=addr(ip='127.0.0.1', port=50740), status='ESTABLISHED', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port=28021), raddr=(), status='LISTEN', pid=6632), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port=8088), raddr=(), status='LISTEN', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=5195), raddr=(), status='NONE', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 2,laddr=addr(ip='::', port=5355), raddr=(), status='NONE', pid=1872), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='::', port=49207), raddr=(), status='LISTEN', pid=6732), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=28989), raddr=(), status='NONE', pid=6632), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50577),raddr=addr(ip='58.215.115.162', port=443), status='CLOSE_WAIT', pid=6280), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 58207),raddr=addr(ip='203.208.50.88', port=80), status='ESTABLISHED', pid=2848), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=49167), raddr=(), status='LISTEN', pid=2176), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='fe80::e4c6:f7c8:d32f:bdaf', port=1801), raddr=(), status='LISTEN', pid=2176), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 58209),raddr=addr(ip='122.226.166.50', port=80), status='ESTABLISHED', pid=2732), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=49157), raddr=(), status='LISTEN', pid=1396), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=2105), raddr=(), status='LISTEN', pid=2176), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50578),raddr=addr(ip='58.215.115.162', port=443), status='CLOSE_WAIT', pid=6280), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=49172), raddr=(), status='LISTEN', pid=288), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=49204), raddr=(), status='LISTEN', pid=256), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=1900), raddr=(), status='NONE', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50616),raddr=addr(ip='101.227.199.28', port=443), status='CLOSE_WAIT', pid=6280), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port=50350), raddr=(), status='LISTEN', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 2,laddr=addr(ip='::', port=5353), raddr=(), status='NONE', pid=8044), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='::', port=49157), raddr=(), status='LISTEN', pid=1396), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=43197), raddr=(), status='NONE', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=20492), raddr=(), status='NONE', pid=6632), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 2,laddr=addr(ip='::', port=500), raddr=(), status='NONE', pid=1396), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=4410), raddr=(), status='LISTEN', pid=2588), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='::', port=49260), raddr=(), status='LISTEN', pid=3368), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 53369),raddr=addr(ip='127.0.0.1', port=53370), status='ESTABLISHED', pid=4816), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=102), raddr=(), status='LISTEN', pid=4364), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='::', port=49153), raddr=(), status='LISTEN', pid=884), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50619),raddr=addr(ip='101.227.199.28', port=443), status='CLOSE_WAIT', pid=6280), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50620),raddr=addr(ip='101.227.199.28', port=443), status='CLOSE_WAIT', pid=6280), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 53372),raddr=addr(ip='127.0.0.1', port=53371), status='ESTABLISHED', pid=4816), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 58211),raddr=addr(ip='115.182.237.20', port=80), status='SYN_SENT', pid=9220), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50618),raddr=addr(ip='101.227.199.28', port=443), status='CLOSE_WAIT', pid=6280), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=49260), raddr=(), status='LISTEN', pid=3368), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 53370),raddr=addr(ip='127.0.0.1', port=53369), status='ESTABLISHED', pid=4816), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 49934),raddr=addr(ip='180.163.21.35', port=80), status='ESTABLISHED', pid=2684), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 58144),raddr=addr(ip='116.211.186.208', port=80), status='CLOSE_WAIT', pid=6632), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 50744),raddr=addr(ip='127.0.0.1', port=50745), status='ESTABLISHED', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port=6677), raddr=(), status='LISTEN', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50621),raddr=addr(ip='101.227.199.28', port=443), status='CLOSE_WAIT', pid=6280), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=17893), raddr=(), status='NONE', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 2,laddr=addr(ip='fe80::e4c6:f7c8:d32f:bdaf', port=546), raddr=(), status='NONE', pid=1304), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=500), raddr=(), status='NONE', pid=1396), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 28021),raddr=addr(ip='127.0.0.1', port=50374), status='ESTABLISHED', pid=6632), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=52136), raddr=(), status='NONE', pid=2684), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 50734),raddr=addr(ip='127.0.0.1', port=50735), status='ESTABLISHED', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 56637),raddr=addr(ip='36.110.238.94', port=80), status='ESTABLISHED', pid=6280), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='::', port=49155), raddr=(), status='LISTEN', pid=1304), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port=9900), raddr=(), status='LISTEN', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50617),raddr=addr(ip='101.227.199.28', port=443), status='CLOSE_WAIT', pid=6280), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type= 1,laddr=addr(ip='::', port=49167), raddr=(), status='LISTEN', pid=2176), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=6000), raddr=(), status='LISTEN', pid=8204), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=60000), raddr=(), status='NONE', pid=6632), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 50738),raddr=addr(ip='127.0.0.1', port=50739), status='ESTABLISHED', pid=4472),
# sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=24515), raddr=(), status='NONE', pid=8460), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=2107), raddr=(), status='LISTEN', pid=2176), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 50739),raddr=addr(ip='127.0.0.1', port=50738), status='ESTABLISHED', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=5353), raddr=(), status='NONE', pid=8044), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 53371),raddr=addr(ip='127.0.0.1', port=53372), status='ESTABLISHED', pid=4816), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port=63342), raddr=(), status='LISTEN', pid=4816), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 58164),raddr=addr(ip='52.114.128.9', port=443), status='ESTABLISHED', pid=10552), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port=5037), raddr=(), status='LISTEN', pid=4812), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=12330), raddr=(), status='NONE', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 50573),raddr=addr(ip='58.215.115.162', port=443), status='CLOSE_WAIT', pid=6280), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='0.0.0.0', port=135), raddr=(), status='LISTEN', pid=1124), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='192.168.31.185', port= 58163),raddr=addr(ip='52.114.128.9', port=443), status='ESTABLISHED', pid=10552), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port=53717), raddr=(), status='LISTEN', pid=8624), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 50736),raddr=addr(ip='127.0.0.1', port=50737), status='ESTABLISHED', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 50374),raddr=addr(ip='127.0.0.1', port=28021), status='ESTABLISHED', pid=4472), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 1,laddr=addr(ip='127.0.0.1', port= 53717),raddr=addr(ip='127.0.0.1', port=53718), status='ESTABLISHED', pid=8624), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=64901), raddr=(), status='NONE', pid=2120), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type= 2,laddr=addr(ip='0.0.0.0', port=12329), raddr=(), status='NONE', pid=6632)]

a=psutil.net_if_addrs()
# {'无线网络连接 2': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='30-B4-9E-87-33-50', netmask=None,
# broadcast=None, ptp=None),
# snicaddr(family=<AddressFamily.AF_INET: 2>, address='169.254.202.69', netmask='255.255.0.0',
# broadcast=None, ptp=None),
# snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::dc9c:c5d4:64b3:ca45', netmask=None,
# broadcast=None, ptp=None)],
# '无线网络连接': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='48-8A-D2-15-BB-A6', netmask=None,
# broadcast=None, ptp=None),
# snicaddr(family=<AddressFamily.AF_INET: 2>, address='192.168.31.185', netmask='255.255.255.0',
#  broadcast=None, ptp=None),
# snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::e4c6:f7c8:d32f:bdaf', netmask=None,
# broadcast=None, ptp=None)],
# '本地连接': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='00-E0-70-47-8F-01', netmask=None,
#  broadcast=None, ptp=None),
# snicaddr(family=<AddressFamily.AF_INET: 2>, address='169.254.246.43', netmask='255.255.0.0', broadcast=None,
# ptp=None),
# snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::518e:c20:68b:f62b', netmask=None,
# broadcast=None, ptp=None)],
# 'isatap.{48C6D664-B85E-474D-B84C-5D5AA3ACA123}': [snicaddr(family=<AddressFamily.AF_LINK: -1>,
# address='00-00-00-00-00-00-00-E0', netmask=None, broadcast=None, ptp=None),
# snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::5efe:192.168.31.185', netmask=None,
#  broadcast=None, ptp=None)],
# 'Teredo Tunneling Pseudo-Interface': [snicaddr(family=<AddressFamily.AF_LINK: -1>,
#  address='00-00-00-00-00-00-00-E0', netmask=None, broadcast=None, ptp=None),
# snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::100:7f:fffe', netmask=None,
# broadcast=None, ptp=None)],
# 'isatap.{6985F0D2-E187-4C05-8D88-166DBFA20A8B}': [snicaddr(family=<AddressFamily.AF_LINK: -1>,
# address='00-00-00-00-00-00-00-E0',
# netmask=None, broadcast=None, ptp=None)], 'isatap.{2C673B69-0B9F-4635-92EB-A8BDC9A7F5CD}':
# [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='00-00-00-00-00-00-00-E0',
# netmask=None, broadcast=None, ptp=None)], 'Loopback Pseudo-Interface 1':
#  [snicaddr(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0',
# broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET6: 23>,
# address='::1', netmask=None, broadcast=None, ptp=None)]}
a=psutil.net_if_stats()

# {'本地连接': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500),
#  'Loopback Pseudo-Interface 1':
# snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=1073, mtu=1500),
#  '无线网络连接': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=4294, mtu=1500),
# '无线网络连接 2': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=4294, mtu=1500),
#  'isatap.{48C6D664-B85E-474D-B84C-5D5AA3ACA123}':
# snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1280),
# 'Teredo Tunneling Pseudo-Interface':
# snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1472),
# 'isatap.{6985F0D2-E187-4C05-8D88-166DBFA20A8B}':
# snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1280),
# 'isatap.{2C673B69-0B9F-4635-92EB-A8BDC9A7F5CD}':
# snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1280)}

# ---------------------------------------------------------------------------------------
2.5进程管理
''''''
d={}
a=psutil.pids()
# print(a,type(a))
# [0, 4, 476, 712, 884, 904, 256, 288, 300, 336, 732, 944, 992, 1124, 1224, 1304, 1364, 1396, 1524, 1572, 1648, 1736, 1768, 1812, 1872, 2036, 1044, 1104, 2068, 2224, 2232, 2280, 2352, 2424, 2588, 2636, 2684, 2704, 2732, 2748, 2856, 2120, 2152, 2176, 3368, 4092, 4364, 4460, 4524, 4720, 5020, 5816, 4308, 3040, 6732, 7660, 8204, 8248, 8588, 5684, 6280, 8460, 4472, 6632, 4812, 9220, 5592, 6556, 4816, 7296, 2368, 8044, 4908, 9648, 9364, 9712, 9816, 10020, 3004, 8272, 9692, 7284, 9416, 5352, 10748, 10828, 10936, 11196, 10572, 8624, 6552, 2000, 2848, 10552, 2644, 10064, 11820, 11588, 12036, 7040, 5872]

k=[(i,psutil.Process(i).name()) for i in psutil.pids()]
# d=dict(zip(a,k))
# print(k)
# [(0, 'System Idle Process'), (4, 'System'), (476, 'smss.exe'), (712, 'csrss.exe'), (884, 'wininit.exe'),
#  (904, 'csrss.exe'), (256, 'services.exe'), (288, 'lsass.exe'), (300, 'lsm.exe'), (336, 'winlogon.exe'),
#  (732, 'svchost.exe'), (944, 'DesktopService64.exe'), (992, 'QQPCRTP.exe'), (1124, 'svchost.exe'),
#  (1224, 'atiesrxx.exe'), (1304, 'svchost.exe'), (1364, 'svchost.exe'), (1396, 'svchost.exe'),
#  (1524, 'audiodg.exe'), (1572, 'svchost.exe'), (1648, 'atieclxx.exe'), (1736, '2345SafeSvc.exe'),
#  (1768, '2345SoftSvc.exe'), (1812, 'Protect_2345Explorer.exe'), (1872, 'svchost.exe'), (2036, 'wlanext.exe'),
#  (1044, 'conhost.exe'), (1104, 'dwm.exe'), (2068, 'explorer.exe'), (2224, 'spoolsv.exe'), (2232, 'taskeng.exe'),
#  (2280, 'taskhost.exe'), (2352, 'svchost.exe'), (2424, 'dllhost.exe'), (2588, 'almsrv64x.exe'),
#  (2636, 'OfficeClickToRun.exe'), (2684, 'QQPCTray.exe'), (2704, '2345SafeTray.exe'), (2732, '2345RTProtect.exe'),
#  (2748, 'ALMPanelPlugin.exe'), (2856, 'RAVCpl64.exe'), (2120, 'DesktopMgr64.exe'), (2152, 'HaoZipSvc.exe'),
#  (2176, 'mqsvc.exe'), (3368, 'sqlservr.exe'), (4092, 'sqlservr.exe'), (4364, 's7oiehsx64.exe'),
#  (4460, 'S7TraceService64x.exe'), (4524, 'svchost.exe'), (4720, 'SearchIndexer.exe'), (5020, 's7epasrv64x.exe'),
#  (5816, 'Shield_2345Explorer.exe'), (4308, 'unsecapp.exe'), (3040, 'WmiPrvSE.exe'), (6732, 'svchost.exe'),
#  (7660, 'KwMusic.exe'), (8204, 'KwService.exe'), (8248, 'KwMusic.exe'), (8588, 'KwMusic.exe'),
#  (5684, 'svchost.exe'), (6280, 'QyClient.exe'), (8460, 'QyPlayer.exe'), (4472, 'QyFragment.exe'),
#  (6632, 'QyKernel.exe'), (4812, 'AndroidService.exe'), (9220, 'WeCap.exe'), (5592, 'rundll32.exe'),
#  (6556, 'SogouCloud.exe'), (4816, 'pycharm64.exe'), (7296, 'fsnotifier64.exe'), (2368, 'conhost.exe'),
#  (8044, 'chrome.exe'), (4908, 'chrome.exe'), (9648, 'chrome.exe'), (9364, 'chrome.exe'), (9712, 'chrome.exe'),
#  (9816, 'chrome.exe'), (10020, 'chrome.exe'), (3004, 'chrome.exe'), (8272, 'chrome.exe'), (9692, 'chrome.exe'),
#  (7284, 'chrome.exe'), (9416, 'chrome.exe'), (5352, 'chrome.exe'), (10748, 'chrome.exe'), (10828, 'chrome.exe'),
#  (10936, 'chrome.exe'), (11196, 'chrome.exe'), (10572, 'chrome.exe'), (8624, 'pythonw.exe'),
#  (6552, 'pythonw.exe'), (2000, 'winchm.exe'), (2848, 'gugefanyiqi_2.2.18.exe'), (10552, 'EXCEL.EXE'),
#  (2644, 'splwow64.exe'), (10064, 'WINWORD.EXE'), (11432, 'ctfmon.exe'), (10652, 'SearchProtocolHost.exe'),
#  (12264, 'SearchFilterHost.exe'),
#  (3916, 'python.exe'), (10560, 'python.exe'), (11436, 'conhost.exe'), (3884, 'conhost.exe')]
p=psutil.Process()
a=p.name()
# print(a)
# python.exe
a=p.username()

# print(a)
# PC-201706181426\Administrator
a=p.cmdline()
# print(a)

# ['C:\\python37\\python.exe', 'C:/python37/Lib/t4.py']
a=p.cwd
# print(a)
# <bound method Process.cwd of psutil.Process(pid=10516, name='python.exe', started='10:58:44')>
# ------------------------------------------------------------------------------------
# 运行进程所在的目录'''

p=psutil.Process(os.getpid())
a=p.cwd()# C:\python37\Lib

p.exe()# C:\python37\python.exe
p.cpu_affinity()# [0, 1, 2, 3]
p.pid# 12124
#
# 父进程pid
p.ppid()# 4816
#
# 返回父进程,如果不存在返回None
a=p.parent()
# psutil.Process(pid=4816, name='pycharm64.exe', started='08:36:48')
a=p.children()# []
a=p.num_threads()# 2
aa=psutil.pids()
k=[(i,psutil.Process(i).name()) for i in aa]
# print(k)

# p=psutil.Process(12184)
a=p.threads()
# [pthread(id=7028, user_time=0.1092007, system_time=0.17160109999999998),
# pthread(id=7040, user_time=0.0, system_time=0.0)]
a=p.status()
# 'running'
a=p.is_running()# True
p.suspend()
a=p.status()# 'stopped'
a=p.is_running()
# print(a)
# True
p.resume()
a=p.status()
# print(a)
# 'running'
a=p.kill()
# print(a)
a=psutil.pid_exists(8248)
# print(a)
# False
psutil.pid_exists(6060)# True
=================================================================================
psutil.test()
#
# USER         PID %MEM     VSZ     RSS TTY           START    TIME  COMMAND
# SYSTEM         0    ?       ?      24 ?             07:33   56:37  System Idle Process
# SYSTEM         4    ?     124     392 ?             07:33   28:22  System
#              256  0.1    6068   10412 ?             07:34   00:08  services.exe
#              288  0.1    5600   13136 ?             07:34   00:20  lsass.exe
#              300    ?    2648    4556 ?             07:34   00:00  lsm.exe
#              336  0.1    3500    8516 ?             07:34   00:00  winlogon.exe
#              476    ?     816    1372 ?             07:34   00:00  smss.exe
#              712  0.1    2504   10220 ?             07:34   00:02  csrss.exe
#              732  0.1    4800   10252 ?             07:34   00:30  svchost.exe
#              884  0.1    1876    5428 ?             07:34   00:00  wininit.exe
#              904  0.4    4188   37536 ?             07:34   01:10  csrss.exe
#              944  0.1    2004    8524 ?             07:34   00:00  DesktopService64.exe
#              992  0.3   68620   27852 ?             07:34   02:55  QQPCRTP.exe
#             1044    ?    1104    3076 ?             07:34   00:00  conhost.exe
# Administra  1104  0.4   36900   44704 ?             07:34   04:55  dwm.exe
#             1124  0.1    4496    8452 ?             07:34   00:03  svchost.exe
#             1224    ?    1732    4700 ?             07:34   00:00  atiesrxx.exe
#             1304  0.2   23392   17852 ?             07:34   00:45  svchost.exe
#             1364  0.2   10088   19520 ?             07:34   00:10  svchost.exe
#             1396  0.4   25556   41116 ?             07:34   01:03  svchost.exe
#             1524  0.3   28832   28044 ?             07:34   08:57  audiodg.exe
#             1572  0.2    9892   21992 ?             07:34   00:08  svchost.exe
#             1648  0.1    2436    6548 ?             07:34   00:00  atieclxx.exe
#             1736  0.1    1984    6308 ?             07:34   00:40  2345SafeSvc.exe
#             1768  0.1    2884    6868 ?             07:34   00:00  2345SoftSvc.exe
#             1812  0.1    7092   14096 ?             07:34   00:05  Protect_2345Explorer.exe
#             1872  0.2   16252   19120 ?             07:34   00:09  svchost.exe
# Administra  2000  0.8   45472   84260 ?             08:38   00:35  winchm.exe
#             2036  0.1    3064    6580 ?             07:34   00:01  wlanext.exe
# Administra  2068  1.0   39668   99660 ?             07:34   00:47  explorer.exe
# Administra  2120  0.7   59260   76396 ?             07:34   00:39  DesktopMgr64.exe
#             2152  0.1    6116   12900 ?             07:34   00:02  HaoZipSvc.exe
#             2176  0.1    4836    8868 ?             07:34   00:00  mqsvc.exe
#             2224  0.1    6272   12124 ?             07:34   00:00  spoolsv.exe
#             2232  0.1    2084    5972 ?             07:34   00:00  taskeng.exe
# Administra  2280  0.2   13580   15864 ?             07:34   00:12  taskhost.exe
#             2352  0.1   11400   14392 ?             07:34   00:35  svchost.exe
# Administra  2368    ?    1608    3952 ?             08:36   00:00  conhost.exe
#             2424  0.1    5384   10480 ?             07:34   00:00  dllhost.exe
#             2588  0.4   26116   37840 ?             07:34   00:00  almsrv64x.exe
#             2636  0.5   32184   48096 ?             07:34   00:06  OfficeClickToRun.exe
# Administra  2644  0.1    1820    5152 ?             09:04   00:00  splwow64.exe
# Administra  2684  1.3   91608  129172 ?             07:34   03:12  QQPCTray.exe
# Administra  2704  0.6   52140   61848 ?             07:34   00:30  2345SafeTray.exe
# Administra  2732  1.5  129472  153124 ?             07:34   42:32  2345RTProtect.exe
#             2748  0.1    1872    5724 ?             07:34   00:00  ALMPanelPlugin.exe
# Administra  2848  0.9   71064   91504 ?             08:46   32:40  gugefanyiqi_2.2.18.exe
# Administra  2856  0.2   17564   23044 ?             07:34   00:00  RAVCpl64.exe
# Administra  3004  1.2   86996  125924 ?             08:37   01:33  chrome.exe
#             3040  0.1    3292    7776 ?             07:34   00:00  WmiPrvSE.exe
#             3368  0.8  100568   83112 ?             07:34   00:03  sqlservr.exe
#             4092  0.8  101180   83256 ?             07:34   00:03  sqlservr.exe
#             4308  0.1    1804    5608 ?             07:34   00:00  unsecapp.exe
#             4364  0.4    5104   39816 ?             07:34   00:00  s7oiehsx64.exe
#             4460  0.2    1860   16240 ?             07:34   00:00  S7TraceService64x.exe
# Administra  4472  1.1  280980  111388 ?             07:50   14:34  QyFragment.exe
#             4524  0.1    2028    6068 ?             07:34   00:00  svchost.exe
#             4720  0.6   71164   61368 ?             07:34   01:31  SearchIndexer.exe
# Administra  4812  0.1    1684    5368 ?             07:51   00:00  AndroidService.exe
# Administra  4816  7.6  789472  780904 ?             08:36   36:26  pycharm64.exe
# Administra  4908  0.1    7692   15036 ?             08:37   00:00  chrome.exe
#             5020  0.1    6148   12448 ?             07:34   00:08  s7epasrv64x.exe
# Administra  5352  0.5   29188   46840 ?             08:37   00:01  chrome.exe
# Administra  5592  0.2    9004   19192 ?             07:53   00:02  rundll32.exe
#             5684  0.1    7460   10840 ?             07:36   00:06  svchost.exe
# Administra  5816  0.2    7168   15628 ?             07:34   00:02  Shield_2345Explorer.exe
# Administra  6280  1.8  160968  188800 ?             07:50   05:21  QyClient.exe
# Administra  6552  0.2   14832   22104 ?             08:37   00:00  pythonw.exe
# Administra  6556  0.2   11048   20636 ?             07:54   00:02  SogouCloud.exe
# Administra  6632  0.8   59424   82948 ?             07:51   06:35  QyKernel.exe
#             6732  0.1    2324    6404 ?             07:34   00:00  svchost.exe
# SYSTEM      7040  0.1    2584    6828 ?             13:52   00:00  SearchFilterHost.exe
# Administra  7284  0.5   34812   51372 ?             08:37   00:00  chrome.exe
# Administra  7296    ?     588    1804 ?             08:36   00:07  fsnotifier64.exe
# Administra  8044  1.1   67252  116688 ?             08:37   01:46  chrome.exe
# Administra  8272  0.7   43708   73208 ?             08:37   00:04  chrome.exe
# Administra  8460  1.4  112240  140200 ?             07:50   17:21  QyPlayer.exe
# Administra  8488  0.1    8776   13532 ?             13:53   00:00  python.exe
#             8588  0.1    4396   10264 ?             13:44   00:06  SearchProtocolHost.exe
# Administra  8624  0.4   27284   42392 ?             08:37   01:18  pythonw.exe
# Administra  8844    ?    1616    4040 ?             13:53   00:00  conhost.exe
# Administra  9220  0.2    8220   15596 ?             07:51   00:00  WeCap.exe
# Administra  9364  0.9   92604   88924 ?             08:37   01:07  chrome.exe
# Administra  9416  0.5   32208   52936 ?             08:37   00:01  chrome.exe
# Administra  9648  0.1    7500   15112 ?             08:37   00:00  chrome.exe
# Administra  9692  0.4   26916   42432 ?             08:37   00:00  chrome.exe
# Administra  9712  1.2   81252  126112 ?             08:37   01:11  chrome.exe
# Administra  9816  0.9   65416   89528 ?             08:37   00:07  chrome.exe
# Administra 10020  0.5   33784   50256 ?             08:37   00:00  chrome.exe
# Administra 10064  2.2  185232  219872 ?             09:36   00:41  WINWORD.EXE
# Administra 10552  1.7  122384  175836 ?             08:46   03:21  EXCEL.EXE
# Administra 10572  0.2   10856   16220 ?             08:37   00:00  chrome.exe
# Administra 10748  0.5   33692   50884 ?             08:37   00:02  chrome.exe
# Administra 10828  0.3   17264   26336 ?             08:37   00:02  chrome.exe
# Administra 10936  0.3   20052   35084 ?             08:37   00:00  chrome.exe
# Administra 11196  0.2   15600   22760 ?             08:37   00:00  chrome.exe
# Administra 11432  0.2    9080   17200 ?             12:14   00:00  ctfmon.exe
# Administra 12128  1.1   82276  107944 ?             13:45   00:10  KwMusic.exe
# Administra 12172    ?   28260    3860 ?             13:45   00:02  KwService.exe
--------------------------------------------------------------------------------------
#
2.6其他信息
# 获取登录用户
a=psutil.users()
print(a)
# [suser(name='Administrator', terminal=None, host='0.0.0.0', started=1537659250.0, pid=None)]
# 获取开机时间
a=psutil.boot_time()
print(a)
# 1537659236.0
import datetime
a=datetime.datetime.fromtimestamp(psutil.boot_time()).strftime('%Y-%m-%d %H:%M:%S')
print(a)
# 2018-09-23 07:33:56
---------------------------------------------------------------------------------------
 
3.其他实例
import psutil,os,time,sys

# 列出所有进程的PID
# print(psutil.pids())

# 实例化一个Process对象,参数为一进程PID
for i in psutil.pids():
p = psutil.Process(i)
print(p.name(),p.pid)

# svchost.exe 5684
# QyClient.exe 6280
# QyPlayer.exe 8460
# ...

''' 
p.name()# svchost.exe 
p.pid#1872 
p.ppid
# <bound method Process.ppid of psutil.Process(pid=1872, name='svchost.exe', started='07:34:10')>
'''
p = psutil.Process(os.getpid())
# 获取进行bin的路径
print(p.exe())# C:\python37\python.exe

# 获取进程工作目录绝对路径
print(p.cwd())# C:\python37\Lib
#
# # 进程的状态
print(p.status())# running
#
# # 进程创建的时间
print(p.create_time())# 1537722012.0
# # 进程uid信息
# print(os.getuid())
#无此方法
# # 进程gid信息
# print(p.gids())
#无此方法
# # 进程CPU时间信息,包括usersystem两个CPU时间
print(p.cpu_times())
# pcputimes(user=0.09360059999999999, system=0.0780005, children_user=0.0, children_system=0.0)
# # 获取进程cpu的亲和度
print(p.cpu_affinity())# [0, 1, 2, 3]
#
## 获取进程内存利用率
print(p.memory_percent())# 0.12982612377070155
# 进程内存rssvms信息
print(p.memory_info())
# pmem(rss=13598720, vms=8568832, num_page_faults=3459, peak_wset=13598720, wset=13598720, peak_paged_pool=154000, paged_pool=154000, peak_nonpaged_pool=14416, nonpaged_pool=14416, pagefile=8568832, peak_pagefile=8568832, private=8568832)

# # 进程IO信息,包括读写IO数及字节数
print(p.io_counters())
# pio(read_count=117, write_count=16, read_bytes=811610, write_bytes=432, other_count=2167, other_bytes=173532)

# 获取打开进程socketnamedutples列表,包括fsfamilyladdr等信息
print(p.connections())# []

# 进程开启的线程数
print(p.num_threads())# 2

-----------------------------------------------------

import psutil

 

for proc in psutil.process_iter():

    try:

        pinfo = proc.as_dict(attrs=['pid', 'name'])

    except psutil.NoSuchProcess:

        pass

    else:

        print(pinfo)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值