Skip to content

Commit 92ff1a1

Browse files
committed
added sys info
1 parent 5fc25c7 commit 92ff1a1

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
2525
- ### [General Python Topics](https://www.thepythoncode.com/topic/general-python-topics)
2626
- [How to Make Facebook Messenger bot in Python](https://www.thepythoncode.com/article/make-bot-fbchat-python). ([code](general/messenger-bot))
2727
- [How to Transfer Files in the Network using Sockets in Python](https://www.thepythoncode.com/article/send-receive-files-using-sockets-python). ([code](general/transfer-files/))
28+
- [How to Get Hardware and System Information in Python](https://www.thepythoncode.com/article/get-hardware-system-information-python). ([code](general/sys-info))
2829

general/sys-info/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [How to Get Hardware and System Information in Python](https://www.thepythoncode.com/article/get-hardware-system-information-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
-
5+
```
6+
python3 sys_info.py
7+
```

general/sys-info/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
psutil

general/sys-info/sys_info.py

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import psutil
2+
import platform
3+
from datetime import datetime
4+
5+
def get_size(bytes, suffix="B"):
6+
"""
7+
Scale bytes to its proper format
8+
e.g:
9+
1253656 => '1.20MB'
10+
1253656678 => '1.17GB'
11+
"""
12+
factor = 1024
13+
for unit in ["", "K", "M", "G", "T", "P"]:
14+
if bytes < factor:
15+
return f"{bytes:.2f}{unit}{suffix}"
16+
bytes /= factor
17+
18+
19+
print("="*40, "System Information", "="*40)
20+
uname = platform.uname()
21+
print(f"System: {uname.system}")
22+
print(f"Node Name: {uname.node}")
23+
print(f"Release: {uname.release}")
24+
print(f"Version: {uname.version}")
25+
print(f"Machine: {uname.machine}")
26+
print(f"Processor: {uname.processor}")
27+
28+
# Boot Time
29+
print("="*40, "Boot Time", "="*40)
30+
boot_time_timestamp = psutil.boot_time()
31+
bt = datetime.fromtimestamp(boot_time_timestamp)
32+
print(f"Boot Time: {bt.year}/{bt.month}/{bt.day} {bt.hour}:{bt.minute}:{bt.second}")
33+
34+
# let's print CPU information
35+
print("="*40, "CPU Info", "="*40)
36+
# number of cores
37+
print("Physical cores:", psutil.cpu_count(logical=False))
38+
print("Total cores:", psutil.cpu_count(logical=True))
39+
# CPU frequencies
40+
cpufreq = psutil.cpu_freq()
41+
print(f"Max Frequency: {cpufreq.max:.2f}Mhz")
42+
print(f"Min Frequency: {cpufreq.min:.2f}Mhz")
43+
print(f"Current Frequency: {cpufreq.current:.2f}Mhz")
44+
# CPU usage
45+
print("CPU Usage Per Core:")
46+
for i, percentage in enumerate(psutil.cpu_percent(percpu=True)):
47+
print(f"Core {i}: {percentage}%")
48+
print(f"Total CPU Usage: {psutil.cpu_percent()}%")
49+
50+
# Memory Information
51+
print("="*40, "Memory Information", "="*40)
52+
# get the memory details
53+
svmem = psutil.virtual_memory()
54+
print(f"Total: {get_size(svmem.total)}")
55+
print(f"Available: {get_size(svmem.available)}")
56+
print(f"Used: {get_size(svmem.used)}")
57+
print(f"Percentage: {svmem.percent}%")
58+
print("="*20, "SWAP", "="*20)
59+
# get the swap memory details (if exists)
60+
swap = psutil.swap_memory()
61+
print(f"Total: {get_size(swap.total)}")
62+
print(f"Free: {get_size(swap.free)}")
63+
print(f"Used: {get_size(swap.used)}")
64+
print(f"Percentage: {swap.percent}%")
65+
66+
# Disk Information
67+
print("="*40, "Disk Information", "="*40)
68+
print("Partitions and Usage:")
69+
# get all disk partitions
70+
partitions = psutil.disk_partitions()
71+
for partition in partitions:
72+
print(f"=== Device: {partition.device} ===")
73+
print(f" Mountpoint: {partition.mountpoint}")
74+
print(f" File system type: {partition.fstype}")
75+
try:
76+
partition_usage = psutil.disk_usage(partition.mountpoint)
77+
except PermissionError:
78+
# this can be catched due to the disk that
79+
# isn't ready
80+
continue
81+
print(f" Total Size: {get_size(partition_usage.total)}")
82+
print(f" Used: {get_size(partition_usage.used)}")
83+
print(f" Free: {get_size(partition_usage.free)}")
84+
print(f" Percentage: {partition_usage.percent}%")
85+
# get IO statistics since boot
86+
disk_io = psutil.disk_io_counters()
87+
print(f"Total read: {get_size(disk_io.read_bytes)}")
88+
print(f"Total write: {get_size(disk_io.write_bytes)}")
89+
90+
# Network information
91+
print("="*40, "Network Information", "="*40)
92+
# get all network interfaces (virtual and physical)
93+
if_addrs = psutil.net_if_addrs()
94+
for interface_name, interface_addresses in if_addrs.items():
95+
for address in interface_addresses:
96+
print(f"=== Interface: {interface_name} ===")
97+
if str(address.family) == 'AddressFamily.AF_INET':
98+
print(f" IP Address: {address.address}")
99+
print(f" Netmask: {address.netmask}")
100+
print(f" Broadcast IP: {address.broadcast}")
101+
elif str(address.family) == 'AddressFamily.AF_PACKET':
102+
print(f" MAC Address: {address.address}")
103+
print(f" Netmask: {address.netmask}")
104+
print(f" Broadcast MAC: {address.broadcast}")
105+
# get IO statistics since boot
106+
net_io = psutil.net_io_counters()
107+
print(f"Total Bytes Sent: {get_size(net_io.bytes_sent)}")
108+
print(f"Total Bytes Received: {get_size(net_io.bytes_recv)}")
109+
110+

0 commit comments

Comments
 (0)