python获取服务器硬件信息_获取服务器硬件详细信息(Python),详情,python

一,获取服务器硬件详情

​ 旨为方便获取服务器详细信息

二, Tips

# python 3.7

socket

psutil

platform

uuid

三, 代码示例

# _*_ coding: utf-8 _*_

import socket

import psutil

import platform

import uuid

class Server(object):

def __init__(self):

pass

@property

def system(self):

hostname = socket.gethostname()

ip = socket.gethostbyname(hostname)

mac = uuid.UUID(int = uuid.getnode()).hex[-12:]

mac = ":".join([mac[e:e + 2] for e in range(0, 11, 2)])

os = platform.system()

return {

'hostname': hostname, # 主机名称

'ip': ip, # IP地址

'mac': mac, # MAC地址

'os': os, # 操作系统

}

@property

def cpu(self):

return {

'count': psutil.cpu_count(logical=False), # 查看cpu物理个数

'percent': str(psutil.cpu_percent(interval=2, percpu=False)) + '%' # CPU的使用率(interval是获取2s内的cpu使用率波动)

}

# 内存

@property

def memory(self):

total = round(psutil.virtual_memory().total / (1024.0 * 1024.0 * 1024.0), 2) # 总物理内存(DDR)

free = round(psutil.virtual_memory().free / (1024.0 * 1024.0 * 1024.0), 2) # 剩余物理内存(DDR)

percent = round((total - free) / total, 2) # 物理内存使用率(DDR)

return {

'free': str(free) + 'G',

'total': str(total) + 'G',

'percent': str(percent * 100) + '%'

}

# 硬盘

@property

def disk(self):

disk_usage = psutil.disk_usage('/')

free = round(disk_usage.free / (1024.0 * 1024.0 * 1024.0), 2)

total = round(disk_usage.total / (1024.0 * 1024.0 * 1024.0), 2)

percent = round((total - free) / total, 2)

return {

'free': str(free) + 'G',

'total': str(total) + 'G',

'percent': str(percent * 100) + '%'

}

if __name__ == '__main__':

info = Server()

print(info.system)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值