본문 바로가기
Python/ETC

[Python] 파이썬 컴퓨터의 다양한 정보 확인하기 (psutil)

by 에파 2024. 8. 12.
728x90

컴퓨터의 성능이나 정보를 확인할 때 'psutil' 이라는 라이브러리를 통해 확인할 수 있습니다. 이번 글에서는 'psutil' 라이브러리의 사용법에 대해 알아보겠습니다.

 

1. 라이브러리 설치

'psutil' 라이브러리를 설치하기 위해 아래 명령어를 입력해주세요.

 

pip install psutil

 

2. CPU 정보 확인

cpu 개수, cpu 사용률, cpu 속도 등을 확인할 수 있습니다.

 

import psutil

# 논리적 CPU 개수
logical_cpu_count = psutil.cpu_count(logical=True)
print(f"논리적 CPU 개수: {logical_cpu_count}")

# 물리적 CPU 개수
physical_cpu_count = psutil.cpu_count(logical=False)
print(f"물리적 CPU 개수: {physical_cpu_count}")

# 전체 CPU 사용률 (퍼센트)
cpu_usage = psutil.cpu_percent(interval=1)
print(f"CPU 사용률: {cpu_usage}%")

# 각 CPU 코어별 사용률
cpu_usage_per_core = psutil.cpu_percent(interval=1, percpu=True)
print(f"코어별 CPU 사용률: {cpu_usage_per_core}")

# CPU 속도
cpu = psutil.cpu_freq()
cpu_current_ghz = round(cpu.current / 1000, 2)
print(f"cpu 속도: {cpu_current_ghz}GHz")

 

3. 메모리 정보 확인

메모리 사용량을 확인할 수 있습니다.

 

# 가상 메모리 정보 확인
virtual_memory = psutil.virtual_memory()
print(f"전체 메모리: {virtual_memory.total / (1024**3):.2f} GB")
print(f"사용 중인 메모리: {virtual_memory.used / (1024**3):.2f} GB")
print(f"사용 가능 메모리: {virtual_memory.available / (1024**3):.2f} GB")
print(f"메모리 사용률: {virtual_memory.percent}%")

#스왑 메모리 정보 확인
swap_memory = psutil.swap_memory()
print(f"스왑 메모리: {swap_memory.total / (1024**3):.2f} GB")
print(f"사용 중인 스왑 메모리: {swap_memory.used / (1024**3):.2f} GB")
print(f"스왑 메모리 사용률: {swap_memory.percent}%")

 

4. 디스크 정보 확인

디스크 사용량 및 파티션 정보를 확인할 수 있습니다.

 

# 디스크 사용량 확인
disk_usage = psutil.disk_usage('/')
print(f"디스크 전체 용량: {disk_usage.total / (1024**3):.2f} GB")
print(f"사용 중인 디스크 용량: {disk_usage.used / (1024**3):.2f} GB")
print(f"남은 디스크 용량: {disk_usage.free / (1024**3):.2f} GB")
print(f"디스크 사용률: {disk_usage.percent}%")

# 디스크 파티션 정보 확인
disk_partitions = psutil.disk_partitions()
for partition in disk_partitions:
    print(f"장치: {partition.device}, 마운트 포인트: {partition.mountpoint}, 파일 시스템 유형: {partition.fstype}")

 

5. 네트워크 정보 확인

네트워크 인터페이스와 통신 상태도 모니터링할 수 있습니다.

 

# 네트워크 인터페이스 정보 확인
net_if_addrs = psutil.net_if_addrs()
for interface_name, interface_addresses in net_if_addrs.items():
    for address in interface_addresses:
        print(f"인터페이스: {interface_name}")
        print(f"  주소 유형: {address.family}")
        print(f"  주소: {address.address}")
        print(f"  넷마스크: {address.netmask}")
        print(f"  브로드캐스트: {address.broadcast}")

# 네트워크 통신 상태 확인
net_io_counters = psutil.net_io_counters()
print(f"송신 바이트: {net_io_counters.bytes_sent}")
print(f"수신 바이트: {net_io_counters.bytes_recv}")

 

6. 프로세스 정보 확인

현재 실행 중인 프로세스와 관련된 정보를 조회할 수 있습니다.

 

# 전체 프로세스 목록 확인
processes = psutil.pids()
print(f"실행 중인 프로세스 ID 목록: {processes}")

# 특정 프로세스 정보 확인
process = psutil.Process(processes[0])  # 첫 번째 프로세스 선택
print(f"프로세스 이름: {process.name()}")
print(f"프로세스 실행 시간: {process.create_time()}")
print(f"프로세스 메모리 정보: {process.memory_info()}")

 

 

 

컴퓨터 정보를 확인하거나 시스템 정보를 모니터링하고 싶다면 'psutil' 라이브러리를 사용해보세요.

댓글