Created
May 23, 2021 03:19
-
-
Save asukaminato0721/5a4209993228b8284bd65daea969fa49 to your computer and use it in GitHub Desktop.
print current net speed and cpu percentage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/a/45064163/13040423 | |
import time | |
import psutil | |
count = 0 | |
qry = "" | |
ul = 0.00 | |
dl = 0.00 | |
t0 = time.time() | |
print(psutil.net_io_counters(pernic=True)) | |
def t(): | |
interface = "vEthernet (虚拟交换机(自动))" | |
return psutil.net_io_counters(pernic=True)[interface] | |
up_down = upload, download = t()[:2] | |
def cpu(): | |
return psutil.cpu_percent(interval=1) | |
while True: | |
t1 = time.time() | |
last_up_down = up_down | |
up_down = upload, download = t()[:2] | |
try: | |
ul, dl = [ | |
(now - last) / (t1 - t0) / 1024.0 | |
for now, last in zip(up_down, last_up_down) | |
] | |
t0 = time.time() | |
except: | |
pass | |
if dl > 0.1 or ul >= 0.1: | |
time.sleep(0.75) | |
print(f"\r{cpu() = } {ul = :0.2f} kB/s {dl = :0.2f} kB/s", end="") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment