python使用WMI檢測(cè)windows系統(tǒng)信息、硬盤信息、網(wǎng)卡信息的方法
更新時(shí)間:2015年05月15日 15:21:03 作者:令狐不聰
這篇文章主要介紹了python使用WMI檢測(cè)windows系統(tǒng)信息、硬盤信息、網(wǎng)卡信息的方法,涉及Python針對(duì)系統(tǒng)信息的相關(guān)操作技巧,需要的朋友可以參考下
本文實(shí)例講述了python使用WMI檢測(cè)windows系統(tǒng)信息、硬盤信息、網(wǎng)卡信息的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wmi
import sys,time,platform
def get_system_info(os):
"""
獲取操作系統(tǒng)版本。
"""
print
print "Operating system:"
if os == "Windows":
c = wmi.WMI ()
for sys in c.Win32_OperatingSystem():
print '\t' + "Version :\t%s" % sys.Caption.encode("GBK")
print '\t' + "Vernum :\t%s" % sys.BuildNumber
def get_memory_info(os):
"""
獲取物理內(nèi)存和虛擬內(nèi)存。
"""
print
print "memory_info:"
if os == "Windows":
c = wmi.WMI ()
cs = c.Win32_ComputerSystem()
pfu = c.Win32_PageFileUsage()
MemTotal = int(cs[0].TotalPhysicalMemory)/1024/1024
print '\t' + "TotalPhysicalMemory :" + '\t' + str(MemTotal) + "M"
#tmpdict["MemFree"] = int(os[0].FreePhysicalMemory)/1024
SwapTotal = int(pfu[0].AllocatedBaseSize)
print '\t' + "SwapTotal :" + '\t' + str(SwapTotal) + "M"
#tmpdict["SwapFree"] = int(pfu[0].AllocatedBaseSize - pfu[0].CurrentUsage)
def get_disk_info(os):
"""
獲取物理磁盤信息。
"""
print
print "disk_info:"
if os == "Windows":
tmplist = []
c = wmi.WMI ()
for physical_disk in c.Win32_DiskDrive():
if physical_disk.Size:
print '\t' + str(physical_disk.Caption) + ' :\t' + str(long(physical_disk.Size)/1024/1024/1024) + "G"
def get_cpu_info(os):
"""
獲取CPU信息。
"""
print
print "cpu_info:"
if os == "Windows":
tmpdict = {}
tmpdict["CpuCores"] = 0
c = wmi.WMI ()
for cpu in c.Win32_Processor():
tmpdict["CpuType"] = cpu.Name
try:
tmpdict["CpuCores"] = cpu.NumberOfCores
except:
tmpdict["CpuCores"] += 1
tmpdict["CpuClock"] = cpu.MaxClockSpeed
print '\t' + 'CpuType :\t' + str(tmpdict["CpuType"])
print '\t' + 'CpuCores :\t' + str(tmpdict["CpuCores"])
def get_network_info(os):
"""
獲取網(wǎng)卡信息和當(dāng)前TCP連接數(shù)。
"""
print
print "network_info:"
if os == "Windows":
tmplist = []
c = wmi.WMI ()
for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
tmpdict = {}
tmpdict["Description"] = interface.Description
tmpdict["IPAddress"] = interface.IPAddress[0]
tmpdict["IPSubnet"] = interface.IPSubnet[0]
tmpdict["MAC"] = interface.MACAddress
tmplist.append(tmpdict)
for i in tmplist:
print '\t' + i["Description"]
print '\t' + '\t' + "MAC :" + '\t' + i["MAC"]
print '\t' + '\t' + "IPAddress :" + '\t' + i["IPAddress"]
print '\t' + '\t' + "IPSubnet :" + '\t' + i["IPSubnet"]
for interfacePerfTCP in c.Win32_PerfRawData_Tcpip_TCPv4():
print '\t' + 'TCP Connect :\t' + str(interfacePerfTCP.ConnectionsEstablished)
if __name__ == "__main__":
os = platform.system()
get_system_info(os)
get_memory_info(os)
get_disk_info(os)
get_cpu_info(os)
get_network_info(os)
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- Python 使用指定的網(wǎng)卡發(fā)送HTTP請(qǐng)求的實(shí)例
- python 的 scapy庫(kù),實(shí)現(xiàn)網(wǎng)卡收發(fā)包的例子
- Python選擇網(wǎng)卡發(fā)包及接收數(shù)據(jù)包
- Python簡(jiǎn)單獲取網(wǎng)卡名稱及其IP地址的方法【基于psutil模塊】
- Python獲取本機(jī)所有網(wǎng)卡ip,掩碼和廣播地址實(shí)例代碼
- Linux服務(wù)器網(wǎng)卡流量查看方法 shell和Python各一枚
- Python腳本實(shí)現(xiàn)網(wǎng)卡流量監(jiān)控
- python監(jiān)控網(wǎng)卡流量并使用graphite繪圖的示例
- python 獲取計(jì)算機(jī)的網(wǎng)卡信息
相關(guān)文章
用Python實(shí)現(xiàn)批量生成法務(wù)函代碼
大家好,本篇文章主要講的是用Python實(shí)現(xiàn)批量生成法務(wù)函代碼,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
利用Python第三方庫(kù)實(shí)現(xiàn)預(yù)測(cè)NBA比賽結(jié)果
今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識(shí),文章圍繞著利用Python實(shí)現(xiàn)預(yù)測(cè)NBA比賽結(jié)果展開,文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-06-06
Python實(shí)現(xiàn)手繪圖效果實(shí)例分享
在本篇文章里小編給大家整理了關(guān)于Python實(shí)現(xiàn)手繪圖效果,有需要的朋友們可以學(xué)習(xí)下。2020-07-07
pytorch?tensor按廣播賦值scatter_函數(shù)的用法
這篇文章主要介紹了pytorch?tensor按廣播賦值scatter_函數(shù)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
python 導(dǎo)入數(shù)據(jù)及作圖的實(shí)現(xiàn)
今天小編就為大家分享一篇python 導(dǎo)入數(shù)據(jù)及作圖的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12

