python psutila模組(示例)

2022-09-08 04:27:12 字數 2257 閱讀 2272

# qianxiao996精心製作

import psutil

import time

import datetime

from prettytable import prettytable

x = prettytable(["名稱", "結果"])

x.align = "l" # 左對齊

x.padding_width = 1 # 列資料左右的空格數量

# 使用者資訊

for u in psutil.users():

x.add_row(["當前使用者", u.name])

x.add_row(["ip 位址", u.host])

date = "當前使用者: " + str(psutil.users()[0][0]) + '\n'

now_time = time.strftime('%y-%m-%d-%h:%m:%s', time.localtime(time.time()))

start_time = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%y-%m-%d %h: %m: %s")

# cpu資訊

cpu_num = psutil.cpu_count(logical=false)

cpu = (str(psutil.cpu_percent(1))) + '%'

# 記憶體資訊

total = str(round(psutil.virtual_memory().total / (1024.0 * 1024.0 * 1024.0), 2)) + 'g'

total_used = str(round(psutil.virtual_memory().used / (1024.0 * 1024.0 * 1024.0), 2)) + 'g'

total_free = str(round(psutil.virtual_memory().free / (1024.0 * 1024.0 * 1024.0), 2)) + 'g'

memory = str(psutil.virtual_memory().percent) + '%'

# 網絡卡資訊

net = psutil.net_io_counters()

bytes_sent = ' mb'.format(net.bytes_recv / 1024 / 1024)

bytes_rcvd = ' mb'.format(net.bytes_sent / 1024 / 1024)

x.add_row(["系統當前時間", now_time])

x.add_row(["系統開機時間", start_time])

x.add_row(["cpu資訊", ""])

x.add_row(["cpu個數", cpu_num])

x.add_row(["cpu使用率", cpu])

x.add_row(["記憶體資訊", ""])

x.add_row(["記憶體總量", total])

x.add_row(["記憶體已使用", total_used])

x.add_row(["剩餘記憶體", total_free])

x.add_row(["記憶體使用率", memory])

x.add_row(["流量資訊", ""])

x.add_row(["網絡卡傳送流量", bytes_sent])

x.add_row(["網絡卡接收流量", bytes_rcvd])

# 磁碟資訊

io = psutil.disk_partitions()

for i in io:

pan = i[0][0][0] + '盤使用情況'

o = psutil.disk_usage(i.device)

disk = str(int(o.total / (1024.0 * 1024.0 * 1024.0))) + "g"

disk_use = str(int(o.used / (1024.0 * 1024.0 * 1024.0))) + "g"

disk_free = str(int(o.free / (1024.0 * 1024.0 * 1024.0))) + "g"

x.add_row([pan, ""])

x.add_row(['總容量', disk])

x.add_row(['已用容量', disk_use])

x.add_row(['可用容量', disk_free])

print( x)

任務模組 報告模組 日誌模組

需求背景 報告模組 怎麼來處理這個報告的問題,報告是非常重要的一塊,1,可以看到每一次執行的情況,多少通過了,多少失敗了 2,可以看到執行的日誌,每一步的日誌,失敗了,我要知道失敗在 了,3,每次都要有報告 業務設計 1,任務列表,任務名稱,檢視任務詳情,報告列表,執行的功能,2,任務詳情,每乙個用...

模組之shutil模組模組詳解

shutil模組是高階的 檔案 資料夾 壓縮包 處理模組 shutil.copyfileobj fsrc,fdst length 將檔案內容拷貝到另乙個檔案中 shutil.copyfile src,dst 拷貝檔案 shutil.copymode src,dst 僅拷貝許可權。內容 組 使用者均不...

常用模組 time模組,datetime模組

print time.time print type time.time 1539594222.698943 時間戳是指從1970年1月1日到現在經歷秒數,返回乙個浮點數。print time.strftime y m d h m s p 需要規定格式 y年 m月 d日 h時 m分 s 分 p上午或...