python如何实现获取linux系统信息-创新互联
python如何实现获取linux系统信息?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

方法一:psutil模块
#!usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import psutil
class NodeResource(object):
def get_host_info(self):
host_name = socket.gethostname()
return {'host_name':host_name}
def get_cpu_state(self):
cpu_count = psutil.cpu_count(logical=False)
cpu_percent =(str)(psutil.cpu_percent(1))+'%'
return {'cpu_count':cpu_count,'cpu_percent':cpu_percent}
def get_memory_state(self):
mem = psutil.virtual_memory()
mem_total = mem.total / 1024 / 1024
mem_free = mem.available /1024/1024
mem_percent = '%s%%'%mem.percent
return {'mem_toal':mem_total,'mem_free':mem_free,'mem_percent':mem_percent}
def get_disk_state(self):
disk_stat = psutil.disk_usage('/')
disk_total = disk_stat.total
disk_free = disk_stat.free
disk_percent = '%s%%'%disk_stat.percent
return {'mem_toal': disk_total, 'mem_free': disk_free, 'mem_percent': disk_percent} 网站名称:python如何实现获取linux系统信息-创新互联
网页路径:http://www.jxjierui.cn/article/codjhc.html


咨询
建站咨询
