2 examples of using Python to get windowns system information, such as CPU, memory, and hard disk _python

Source: Internet
Author: User
Tags sleep

Example one:

Python uses the WMI module to obtain hardware information for the WINDOWNS system: Hard disk partitions, usage, memory size, CPU model, current running process, Self starter program and location, system version, and more.

Copy Code code as follows:

#!/usr/bin/env python
#-*-Coding:utf-8-*-

Import WMI
Import OS
Import Sys
Import Platform
Import time

Def sys_version ():
c = WMI. WMI ()
#获取操作系统版本
For SYS in C.win32_operatingsystem ():
Print "version:%s"% sys. Caption.encode ("UTF8"), "vernum:%s"% sys. BuildNumber
Print sys. Osarchitecture.encode ("UTF8") #系统是32位还是64位的
Print sys. Numberofprocesses #当前系统运行的进程总数

Def cpu_mem ():
c = WMI. WMI ()
#CPU类型和内存
For processor in C.win32_processor ():
#print ' Processor ID:%s '% Processor. DeviceID
print ' Process Name:%s '% processor. Name.strip ()
For Memory in C.win32_physicalmemory ():
Print "Memory Capacity:%.FMB"% (int (memory.capacity)/1048576)

Def cpu_use ():
#5s取一次CPU的使用率
c = WMI. WMI ()
While True:
For CPUs in C.win32_processor ():
timestamp = Time.strftime ('%a,%d%b%Y%h:%m:%s ', Time.localtime ())
print '%s | Utilization:%s:%d%% (timestamp, CPU). DeviceID, CPU. Loadpercentage)
Time.sleep (5)

def disk ():
c = WMI. WMI ()
#获取硬盘分区
For Physical_disk in C.win32_diskdrive ():
For partition in Physical_disk.associators ("Win32_diskdrivetodiskpartition"):
For Logical_disk in Partition.associators ("Win32_logicaldisktopartition"):
Print Physical_disk. Caption.encode ("UTF8"), partition. Caption.encode ("UTF8"), Logical_disk. Caption

#获取硬盘使用百分情况
For disk in C.win32_logicaldisk (drivetype=3):
Print disk. Caption, "%0.2f%% Free"% (100.0 * Long (disk). FreeSpace)/long (disk. Size))

Def network ():
c = WMI. WMI ()
#获取MAC和IP地址
For interface in C.win32_networkadapterconfiguration (ipenabled=1):
Print "MAC:%s"% interface. MACAddress
For ip_address in interface. IPAddress:
print ' Ip_add:%s '% ip_address
Print

#获取自启动程序的位置
For S in C.win32_startupcommand ():
print ' [%s]%s <%s> '% (S.location.encode ("UTF8"), S.caption.encode ("UTF8"), S.command.encode ("UTF8"))


#获取当前运行的进程
For process in c.win32_process ():
Print process. ProcessID, process. Name

def main ():
Sys_version ()
#cpu_mem ()
#disk ()
#network ()
#cpu_use ()

if __name__ = = ' __main__ ':
Main ()
Print Platform.system ()
Print Platform.release ()
Print platform.version ()
Print Platform.platform ()
Print Platform.machine ()

Example two:

Because I'm not using much, I only get CPU, memory, and hard drives, and if I need additional resources, please refer to MSDN.

Copy Code code as follows:

Import OS
Import Win32API
Import Win32con
Import WMI
Import time

def getsysinfo (wmiservice = None):
result = {}
If Wmiservice = None:
Wmiservice = WMI. WMI ()
# CPU
For CPUs in Wmiservice.win32_processor ():
timestamp = Time.strftime ('%a,%d%b%Y%h:%m:%s ', Time.localtime ())
result[' cpupercent '] = Cpu.loadpercentage
# memory
cs = Wmiservice.win32_computersystem ()
OS = Wmiservice.win32_operatingsystem ()
result[' memtotal ' = int (int (cs[0). TotalPhysicalMemory)/1024/1024)
result[' memfree ' = int (int (os[0). freephysicalmemory)/1024)
#disk
result[' disktotal '] = 0
result[' diskfree '] = 0
For disk in Wmiservice.win32_logicaldisk (drivetype=3):
result[' disktotal '] = = Int (disk. Size)
result[' diskfree '] = = Int (disk. FreeSpace)
result[' disktotal ' = Int (result[' disktotal ']/1024/1024)
result[' diskfree ' = Int (result[' diskfree ']/1024/1024)
return result

if __name__ = = ' __main__ ':
Wmiservice = WMI. WMI ()
While True:
Print Getsysinfo (wmiservice)
Time.sleep (3)

The WMI module acquires, because WMI is initialized with too much system resources, so if you need to recycle it, initialize the WMI object outside the loop body, and then pass in the function so that there is no high CPU resource.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.