python读取计算机硬件信息代码

你可以使用Python的第三方库来读取计算机硬件信息。以下是一些常用的库和示例代码:

1. psutil:用于获取系统信息,包括CPU、内存、磁盘、网络等。

```python
import psutil

# 获取CPU信息
cpu_percent = psutil.cpu_percent()
cpu_count = psutil.cpu_count()
cpu_freq = psutil.cpu_freq()

# 获取内存信息
mem_total = psutil.virtual_memory().total
mem_used = psutil.virtual_memory().used
mem_percent = psutil.virtual_memory().percent

# 获取磁盘信息
disk_usage = psutil.disk_usage('/')
disk_total = disk_usage.total
disk_used = disk_usage.used
disk_percent = disk_usage.percent

# 获取网络信息
net_io_counters = psutil.net_io_counters()
net_bytes_sent = net_io_counters.bytes_sent
net_bytes_recv = net_io_counters.bytes_recv
```

2. wmi:用于获取Windows系统信息,包括CPU、内存、磁盘、网络等。

```python
import wmi

# 连接WMI服务
c = wmi.WMI()

# 获取CPU信息
for processor in c.Win32_Processor():
    cpu_name = processor.Name
    cpu_architecture = processor.Architecture
    cpu_cores = processor.NumberOfCores
    cpu_threads = processor.NumberOfLogicalProcessors

# 获取内存信息
for mem in c.Win32_PhysicalMemory():
    mem_capacity = mem.Capacity
    mem_speed = mem.Speed

# 获取磁盘信息
for disk in c.Win32_LogicalDisk(DriveType=3):
    disk_name = disk.Caption
    disk_total = int(disk.Size) / (1024 ** 3)
    disk_free = int(disk.FreeSpace) / (1024 ** 3)

# 获取网络信息
for interface in c.Win32_NetworkAdapterConfiguration(IPEnabled=1):
    net_mac = interface.MACAddress
    net_ip = interface.IPAddress[0]
```

3. py-cpuinfo:用于获取CPU信息。

```python
import cpuinfo

# 获取CPU信息
info = cpuinfo.get_cpu_info()
cpu_name = info['brand_raw']
cpu_architecture = info['arch']
cpu_cores = info['count']
```

4. psutil-sensors:用于获取传感器信息,包括CPU温度、风扇转速等。

```python
import psutil_sensors

# 获取传感器信息
sensors = psutil_sensors.Sensors()
cpu_temp = sensors['coretemp'][0].current
fan_speed = sensors['nct6798'][0].fans[0].current
```

你可能感兴趣的:(python,开发语言)