首页 / 国外VPS推荐 / 正文
Python 获取服务器性能,全面指南,python获取服务器数据

Time:2025年02月10日 Read:8 评论:42 作者:y21dr45

在现代 IT 运维和开发中,了解服务器的性能状态是至关重要的,Python 提供了多种方式来获取服务器的性能数据,无论是 CPU、内存、磁盘还是网络使用情况,本文将详细介绍如何使用 Python 获取服务器性能信息,并提供相应的代码示例。

Python 获取服务器性能,全面指南,python获取服务器数据

获取 CPU 使用率

要获取 CPU 的使用率,可以使用psutil 库,这是一个跨平台库,能够方便地检索系统利用率(包括 CPU、内存、磁盘等)信息。

确保你已经安装了psutil 库:

pip install psutil

你可以使用以下代码获取 CPU 使用率:

import psutil
获取 CPU 整体使用率
cpu_usage = psutil.cpu_percent(interval=1)
print(f"CPU Usage: {cpu_usage}%")
获取每个 CPU 核心的使用率
cpu_times = psutil.cpu_times_percent(interval=1, percpu=True)
for i, usage in enumerate(cpu_times):
    print(f"CPU Core {i} Usage: {usage}%")

获取内存使用率

同样地,我们可以使用psutil 来获取内存的使用情况:

获取内存整体使用率
memory = psutil.virtual_memory()
print(f"Memory Usage: {memory.percent}%")
获取详细的内存信息
print(f"Total Memory: {memory.total / (1024 ** 3)} GB")
print(f"Used Memory: {memory.used / (1024 ** 3)} GB")
print(f"Free Memory: {memory.free / (1024 ** 3)} GB")

获取磁盘使用率

对于磁盘使用率,我们也可以借助psutil

获取磁盘整体使用率
disk_usage = psutil.disk_usage('/')
print(f"Disk Usage: {disk_usage.percent}%")
获取详细的磁盘信息
print(f"Total Disk Space: {disk_usage.total / (1024 ** 3)} GB")
print(f"Used Disk Space: {disk_usage.used / (1024 ** 3)} GB")
print(f"Free Disk Space: {disk_usage.free / (1024 ** 3)} GB")

获取网络使用率

获取网络使用情况稍微复杂一点,但依然可以通过psutil 来实现:

获取网络 I/O 统计信息
net_io = psutil.net_io_counters()
print(f"Bytes Sent: {net_io.bytes_sent / (1024 ** 2)} MB")
print(f"Bytes Received: {net_io.bytes_recv / (1024 ** 2)} MB")
获取网络接口的详细信息
for interface, addrs in psutil.net_if_addrs().items():
    print(f"Interface: {interface}")
    for addr in addrs:
        print(f"  Address: {addr.address} Netmask: {addr.netmask} Broadcast IP: {addr.broadcast}")

综合监控脚本

为了更全面地监控服务器性能,可以编写一个综合脚本,定期输出各类性能指标:

import psutil
import time
def get_system_info():
    # CPU 使用率
    cpu_usage = psutil.cpu_percent(interval=1)
    print(f"CPU Usage: {cpu_usage}%")
    
    # 内存使用率
    memory = psutil.virtual_memory()
    print(f"Memory Usage: {memory.percent}%")
    print(f"Total Memory: {memory.total / (1024 ** 3)} GB")
    print(f"Used Memory: {memory.used / (1024 ** 3)} GB")
    print(f"Free Memory: {memory.free / (1024 ** 3)} GB")
    
    # 磁盘使用率
    disk_usage = psutil.disk_usage('/')
    print(f"Disk Usage: {disk_usage.percent}%")
    print(f"Total Disk Space: {disk_usage.total / (1024 ** 3)} GB")
    print(f"Used Disk Space: {disk_usage.used / (1024 ** 3)} GB")
    print(f"Free Disk Space: {disk_usage.free / (1024 ** 3)} GB")
    
    # 网络使用率
    net_io = psutil.net_io_counters()
    print(f"Bytes Sent: {net_io.bytes_sent / (1024 ** 2)} MB")
    print(f"Bytes Received: {net_io.bytes_recv / (1024 ** 2)} MB")
    
    for interface, addrs in psutil.net_if_addrs().items():
        print(f"Interface: {interface}")
        for addr in addrs:
            print(f"  Address: {addr.address} Netmask: {addr.netmask} Broadcast IP: {addr.broadcast}")
    print("="*40)
if __name__ == "__main__":
    while True:
        get_system_info()
        time.sleep(5)  # 每5秒刷新一次数据

通过本文的介绍,你应该已经掌握了如何使用 Python 获取服务器的各种性能指标,利用psutil 库,我们可以方便地获取 CPU、内存、磁盘和网络的使用情况,从而对服务器的健康状态进行实时监控,这些信息对于系统管理员和开发人员来说都是非常有价值的,可以帮助他们及时发现并解决潜在的问题。

排行榜
关于我们
「好主机」服务器测评网专注于为用户提供专业、真实的服务器评测与高性价比推荐。我们通过硬核性能测试、稳定性追踪及用户真实评价,帮助企业和个人用户快速找到最适合的服务器解决方案。无论是云服务器、物理服务器还是企业级服务器,好主机都是您值得信赖的选购指南!
快捷菜单1
服务器测评
VPS测评
VPS测评
服务器资讯
服务器资讯
扫码关注
鲁ICP备2022041413号-1