kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

python如何读取硬件信息

https://docs.pingcode.com/ask/ask-ask/954442.html

py-cpuinfo库

py-cpuinfo是一个专门用于获取CPU信息的Python库。它可以提供更详细的CPU信息,如CPU型号、品牌、架构等。

1
pip install py-cpuinfo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import cpuinfo

info = cpuinfo.get_cpu_info()

brand = info['brand_raw']
print(f"CPU品牌: {brand}")

arch = info['arch']
print(f"CPU架构: {arch}")

hz_advertised = info['hz_advertised']
print(f"CPU频率: {hz_advertised[0]}")

count = info['count']
print(f"CPU核心数量: {count}")

PYTHON读取内存信息

Python中使用psutil库可以很方便地读取系统内存的信息,包括总内存、可用内存、已使用内存和内存使用率等。

1
pip install psutil

获取虚拟内存信息

1
2
3
4
5
6
7
import psutil
virtual_memory = psutil.virtual_memory()

print(f"总内存: {virtual_memory.total / (10243):.2f} GB")
print(f"可用内存: {virtual_memory.avAIlable / (10243):.2f} GB")
print(f"已使用内存: {virtual_memory.used / (10243):.2f} GB")
print(f"内存使用率: {virtual_memory.percent}%")

获取交换内存信息

1
2
3
4
5
swap_memory = psutil.swap_memory()

print(f"总交换内存: {swap_memory.total / (10243):.2f} GB")
print(f"已使用交换内存: {swap_memory.used / (10243):.2f} GB")
print(f"交换内存使用率: {swap_memory.percent}%")

三、PYTHON读取磁盘信息

Python中可以使用psutil库来读取磁盘信息,包括磁盘分区、使用情况和I/O统计等信息。

1
pip install psutil

获取磁盘分区信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import psutil

partitions = psutil.disk_partitions()

for partition in partitions:
	print(f"设备: {partition.device}")
	print(f"挂载点: {partition.mountpoint}")
	print(f"文件系统类型: {partition.fstype}")
	# 获取磁盘使用情况
	usage = psutil.disk_usage(partition.mountpoint)
	print(f"总空间: {usage.total / (10243):.2f} GB")
	print(f"已用空间: {usage.used / (10243):.2f} GB")
	print(f"可用空间: {usage.free / (10243):.2f} GB")
	print(f"使用率: {usage.percent}%\n")

获取磁盘I/O统计信息

1
2
3
4
5
6
disk_io = psutil.disk_io_counters()

print(f"读操作次数: {disk_io.read_count}")
print(f"写操作次数: {disk_io.write_count}")
print(f"读字节数: {disk_io.read_bytes / (10242):.2f} MB")
print(f"写字节数: {disk_io.write_bytes / (10242):.2f} MB")

四、PYTHON读取网络信息

Python中可以使用psutil库来读取网络信息,包括网络接口、流量统计和连接信息等。

1
pip install psutil

获取网络接口信息

1
2
3
4
5
6
7
8
9
10
11
12
import psutil

network_interfaces = psutil.net_if_addrs()

for interface_name, interface_addresses in network_interfaces.items():
print(f"接口: {interface_name}")
for address in interface_addresses:
	if str(address.family) == 'AddressFamily.AF_INET':
		print(f"  IP地址: {address.address}")
		print(f"  子网掩码: {address.netmask}")
	elif str(address.family) == 'AddressFamily.AF_PACKET':
		print(f"  MAC地址: {address.address}")

获取网络流量统计信息

1
2
3
4
net_io = psutil.net_io_counters()

print(f"发送字节数: {net_io.bytes_sent / (10242):.2f} MB")
print(f"接收字节数: {net_io.bytes_recv / (10242):.2f} MB")

获取网络连接信息

1
2
3
network_connections = psutil.net_connections()

print(f"当前网络连接数: {len(network_connections)}")

五、PYTHON读取GPU信息

对于GPU信息的获取,Python可以使用GPUtil库。该库提供了一个简单的接口来访问NVIDIA GPU的信息,如GPU名称、负载、温度和内存使用情况等。

1
pip install gputil

获取所有GPU的信息

1
2
3
4
5
6
7
8
9
10
11
12
import GPUtil

gpus = GPUtil.getGPUs()

for gpu in gpus:
	print(f"GPU ID: {gpu.id}")
	print(f"GPU名称: {gpu.name}")
	print(f"GPU负载: {gpu.load * 100}%")
	print(f"GPU温度: {gpu.temperature} °C")
	print(f"GPU总显存: {gpu.memoryTotal} MB")
	print(f"GPU已用显存: {gpu.memoryUsed} MB")
	print(f"GPU空闲显存: {gpu.memoryFree} MB\n")

系统信息

https://www.php.cn/faq/2608362.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import os
import socket
import platform

u = platform.uname()
if u.system == 'Linux':
	kernel = u.release
elif u.system == 'Windows':
	kernel = f'Windows {u.release}.{platform.version()}'
print("系统:", kernel)

username = os.getlogin()
print("用户名:", username)

hostname = socket.gethostname()
print("操作系统主机名:", hostname)

language, python

« Python获取运行目录的5种方法及实现 python用winreg读取注册表 »