python如何实现获取linux系统信息-创新互联

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

创新互联建站主要从事网站建设、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务赞皇,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

方法一: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://pwwzsj.com/article/codjhc.html