Python uses the WMI module to obtain windowns system information

Source: Internet
Author: User

Python uses the WMI module to obtain the hardware information of the windowns system: hard disk partition, usage, memory size, CPU model, current running process, self-starting program and location, and system version.

 
 
  1. #! /Usr/bin/envpython
  2. #-*-Coding: UTF-8 -*-
  3. Importwmi
  4. Importos
  5. Importsys
  6. Importplatform
  7. Importtime
  8. Defsys_version ():
  9. C = wmi. WMI ()
  10. # Obtain the operating system version
  11. Forsysinc. Win32_OperatingSystem ():
  12. Print "Version: % s" % sys. Caption. encode ("UTF8"), "Vernum: % s" % sys. BuildNumber
  13. Printsys. OSArchitecture. encode ("UTF8") # whether the system is 32-bit or 64-bit
  14. Printsys. NumberOfProcesses # Total number of processes running on the current system
  15. Defcpu_mem ():
  16. C = wmi. WMI ()
  17. # CPU type and memory
  18. Forprocessorinc. Win32_Processor ():
  19. # Print "ProcessorID: % s" % processor. DeviceID
  20. Print "ProcessName: % s" % processor. Name. strip ()
  21. ForMemoryinc. Win32_PhysicalMemory ():
  22. Print "MemoryCapacity: %. fMB" % (int (Memory. Capacity)/1048576)
  23. Defcpu_use ():
  24. # CPU usage per 5 seconds
  25. C = wmi. WMI ()
  26. WhileTrue:
  27. Forcpuinc. Win32_Processor ():
  28. Timestamp = time. strftime ('% a, % d % B % Y % H: % M: % s', time. localtime ())
  29. Print '% s | Utilization: % s: % d %' % (timestamp, cpu. DeviceID, cpu. LoadPercentage)
  30. Time. sleep (5)
  31. Defdisk ():
  32. C = wmi. WMI ()
  33. # Obtain Hard Disk Partitions
  34. Forphysical_diskinc.Win32_DiskDrive ():
  35. Forpartitioninphysical_disk.associators ("Win32_DiskDriveToDiskPartition "):
  36. Forlogical_diskinpartition.associators ("Win32_LogicalDiskToPartition "):
  37. Printphysical_disk.Caption.encode ("UTF8"), partition. Caption. encode ("UTF8"), logical_disk.Caption
  38. # Obtain the disk usage percentage
  39. Fordiskinc. Win32_LogicalDisk (DriveType = 3 ):
  40. Printdisk. Caption, "% 0.2f % free" % (100.0 * long (disk. FreeSpace)/long (disk. Size ))
  41. Defnetwork ():
  42. C = wmi. WMI ()
  43. # Obtain MAC and IP addresses
  44. Forinterfaceinc. Win32_NetworkAdapterConfiguration (IPEnabled = 1 ):
  45. Print "MAC: % s" % interface. MACAddress
  46. Forip_addressininterface.IPAddress:
  47. Print "ip_add: % s" % ip_address
  48. Print
  49. # Obtain the position of the self-starting Program
  50. Forsinc. Win32_StartupCommand ():
  51. Print "[% s] % s <% s>" % (s. location. encode ("UTF8"), s. caption. encode ("UTF8"), s. command. encode ("UTF8 "))
  52. # Obtain the currently running process
  53. Forprocessinc. Win32_Process ():
  54. Printprocess. ProcessId, process. Name
  55. Defmain ():
  56. Sys_version ()
  57. # Cpu_mem ()
  58. # Disk ()
  59. # Network ()
  60. # Cpu_use ()
  61. If _ name __= = '_ main __':
  62. Main ()
  63. Printplatform. system ()
  64. Printplatform. release ()
  65. Printplatform. version ()
  66. Printplatform. platform ()
  67. Printplatform. machine ()

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.