CPU, 메모리, 디스크, 네트워크 등의 정보를 확인하는 컴퓨터의 정보를 확인하는 프로그램을 만들어본다.
psutil은 컴퓨터의 정보를 확인할 때 사용하는 라이브러리, 아나콘다가 설치될 때 함께 설치되는 라이브러리이다.
pip install psutil
import psutil
cpu = psutil.cpu_freq()
print(cpu) # cpu의 속도 출력
cpu_core = psutil.cpu_count(logical=False)
print(cpu_core) # cpu의 물리코어 수를 출력
memory = psutil.virtual_memory()
print(memory) # 메모리의 정보 출력
disk = psutil.disk_partitions()
print(disk) # 디스크 정보 출력
net = psutil.net_io_counters()
print(net) # 네트워크를 통해 보내고 받은 데이터량을 출력
import psutil
cpu = psutil.cpu_freq()
cpu_current_ghz = round(cpu.current / 1000, 2)
print(f"cpu속도: {cpu_current_ghz}GHz")
cpu_core = psutil.cpu_count(logical=False)
print(f"코어: {cpu_core} 개")
memory = psutil.virtual_memory()
memory_total = round(memory.total / 1024**3)
print(f'메모리: {memory_total}GB')
disk = psutil.disk_partitions()
for p in disk:
print(p.mountpoint, p.fstype, end=' ')
du = psutil.disk_usage(p.mountpoint)
disk_total = round(du.total / 1024**3)
print(f'디스크크기: {disk_total}GB')
# 디스크는 하나가 아닌 여러 개가 있을 수 있으므로 찾은 수 만큼 출력한다.
net = psutil.net_io_counters()
sent = round(net.bytes_sent/1024**2, 1)
recv = round(net.bytes_recv/1024**2, 1)
print(f'보내기: {sent}MB 받기: {recv}MB')
# 네트워크를 통해 보내고 받은 데이터를 크기를 MB단위로 출력한다. 보내고 받은 데이터는 누적데이터의 값이다.
import psutil
curr_sent = 0
curr_recv = 0
prev_sent = 0
prev_recv = 0
while True:
cpu_p = psutil.cpu_percent(interval=1)
# cpu의 사용량을 1초동의 평균값을 구한다. interval의 시간을 조절하여 평균 구하는 시간을 조절한다.
print(f'CPU사용량: {cpu_p}%')
memory = psutil.virtual_memory()
memory_avail = round(memory.available/1024**3, 1)
print(f'사용가능한 메모리: {memory_avail}GB')
net = psutil.net_io_counters()
curr_sent = net.bytes_sent/1024**2
curr_recv = net.bytes_recv/1024**2
# 현재의 값에서 이전의 값을 빼면 1초동안 보내는 데이터를 구할 수 있다.
sent = round(curr_sent-prev_sent, 1)
recv = round(curr_recv-prev_recv, 1)
print(f'보내기: {sent}MB 받기: {recv}MB')
# 이전의 값에 현재 값을 바인딩한다. 이전의 값을 가지고 있어야 현재값과 비교하여 1초 동안 얼마를 보내고 받았는지 계산 가능.
prev_sent = curr_sent
prev_recv = curr_recv