阅读量:4
要使用Python的psutil库获取系统信息,首先需要安装psutil库
pip install psutil
接下来,你可以使用以下代码示例来获取各种系统信息:
import psutil
# 获取系统名称
system_name = psutil.os.name()
print(f"系统名称: {system_name}")
# 获取系统版本
system_version = psutil.os.version()
print(f"系统版本: {system_version}")
# 获取系统架构
system_architecture = psutil.os.arch()
print(f"系统架构: {system_architecture}")
# 获取CPU信息
cpu_info = psutil.cpu_info()
print(f"CPU信息: {cpu_info}")
# 获取CPU使用率
cpu_usage = psutil.cpu_percent()
print(f"CPU使用率: {cpu_usage}%")
# 获取内存信息
memory_info = psutil.virtual_memory()
print(f"内存信息: {memory_info}")
# 获取磁盘信息
disk_info = psutil.disk_usage('/')
print(f"磁盘信息: {disk_info}")
# 获取网络接口信息
network_interface_info = psutil.net_if_addrs()
print(f"网络接口信息: {network_interface_info}")
# 获取当前进程列表
process_list = psutil.process_iter(['pid', 'name', 'cpu_percent'])
print("当前进程列表:")
for process in process_list:
print(process)
这个代码示例将输出一些基本的系统信息,如系统名称、版本、架构、CPU和内存使用情况、磁盘空间和网络接口信息。你可以根据需要调整代码以获取更多详细信息。
以上就是关于“psutil python如何获取系统信息”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm