關於Python 獲取windows信息收集

 

收集一些Python操做windows的代碼 (不論是自帶的or第三方庫)均來自網上python

1.shutdown 操做windows

定時關機、重啓、註銷app

#!/usr/bin/python
#-*-coding:utf-8-*-
#shutdown.py

import sys#導入
import os

from PyQt4.QtCore import *
from PyQt4.QtGui import *

   
class ShutDown(QWidget):
    def __init__(self):
        super(ShutDown, self).__init__()
        self.setWindowTitle('PyQt')
        self.hourLabel = QLabel(u'小時')#標籤
        self.minuteLabel = QLabel(u'分鐘')
        self.secondLabel = QLabel(u'')
        
        self.shutDownButton = QPushButton()#按鈕
        self.shutDownButton.setText(u'關機')
        self.shutDownButton.clicked.connect(self.confirm_shutdown)
        self.rebootButton = QPushButton()
        self.rebootButton.setText(u'重啓')
        self.rebootButton.clicked.connect(self.confirm_reboot)
        self.logoffButton = QPushButton()
        self.logoffButton.setText(u'註銷')
        self.logoffButton.clicked.connect(self.confirm_logoff)
        
        self.hourSpinBox = QSpinBox()
        self.hourSpinBox.setRange(0,1000)
        self.hourSpinBox.setSingleStep(1)
        self.hourSpinBox.setValue(0)
        self.minuteSpinBox = QSpinBox()
        self.minuteSpinBox.setRange(0,1000)
        self.minuteSpinBox.setSingleStep(1)
        self.minuteSpinBox.setValue(0)
        self.secondSpinBox = QSpinBox()
        self.secondSpinBox.setRange(0,1000)
        self.secondSpinBox.setSingleStep(1)
        self.secondSpinBox.setValue(0)
        
        self.layout = QGridLayout()#網格佈局
        self.layout.addWidget(self.hourLabel,1,0)
        self.layout.addWidget(self.hourSpinBox,1,1)
        self.layout.addWidget(self.minuteLabel,1,2)
        self.layout.addWidget(self.minuteSpinBox,1,3)
        self.layout.addWidget(self.secondLabel,1,4)
        self.layout.addWidget(self.secondSpinBox,1,5)
        self.layout.addWidget(self.shutDownButton,2,0,2,2)
        self.layout.addWidget(self.rebootButton,2,2,2,2)
        self.layout.addWidget(self.logoffButton,2,4,2,2)
        self.setLayout(self.layout)


    def confirm_shutdown(self):
        self.time = (self.hourSpinBox.value() * 3600 + self.minuteSpinBox.value() * 60
                     + self.secondSpinBox.value())
        shutdown = 'C:\Windows\System32\shutdown.exe -s -t ' + str(self.time)
        os.system(shutdown)
        sys.exit(0)
        
    def confirm_reboot(self):
        reboot = 'C:\Windows\System32\shutdown.exe -r' 
        os.system(reboot)
        sys.exit(0)
        
    def confirm_logoff(self):
        logoff = 'C:\Windows\System32\shutdown.exe -l'
        os.system(logoff)
        sys.exit(0)
        
  
    
if __name__ == '__main__':
    app = QApplication(sys.argv)
    shutdown = ShutDown()
    shutdown.show()
    sys.exit(app.exec_())

 

2.windows鎖屏佈局

至關與WIN+L 操做ui

 1 # -*- coding: utf-8 -*-
 2 # python在windows鎖屏的代碼
 3 import sys
 4 from ctypes import *
 5 from PyQt4 import QtGui, QtCore
 6 try:
 7     _fromUtf8 = QtCore.QString.fromUtf8
 8 except AttributeError:
 9     def _fromUtf8(s):
10         return s
11 
12 class QuitButton(QtGui.QWidget):
13     def __init__(self, parent=None):
14         QtGui.QWidget.__init__(self, parent)
15         self.setGeometry(300, 300, 250, 150)
16         self.setWindowTitle(_fromUtf8('windows鎖屏'))    #utf8 輸出中文
17         lockscreen = QtGui.QPushButton('OK', self)
18         lockscreen.setGeometry(10, 10, 60, 35)
19         QtCore.QObject.connect(lockscreen, QtCore.SIGNAL('clicked()'), self.win_L)  #self.win_L  點擊執行
20     def win_L(self):
21         user32 = windll.LoadLibrary('user32.dll')
22         user32.LockWorkStation()
23 
24 class lockScr(QtGui.QWidget):
25     def __init__(self):
26         QtGui.QWidget.__init__(self)
27     def win_L(self):
28         user32 = windll.LoadLibrary('user32.dll')
29         user32.LockWorkStation()
30 
31 app = QtGui.QApplication(sys.argv)
32 qb = QuitButton()
33 qb.show()
34 sys.exit(app.exec_())

 

3.獲取電腦的基本信息spa

引用第三方庫WMI 能夠查看到 windows當前的版本、位數X86(64)、進程數目、CPU類型和使用率、硬盤的分區和使用率、Ip地址和Mac地址、 詳細進程數據等操作系統

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 import wmi
 5 import os
 6 import sys
 7 import platform
 8 import time
 9 import sys
10 reload(sys)
11 sys.setdefaultencoding( "utf-8" )
12 def sys_version():
13     c = wmi.WMI ()
14     #獲取操做系統版本
15     for sys in c.Win32_OperatingSystem():
16         print u"Version:%s" % sys.Caption,"Vernum:%s" % sys.BuildNumber
17         print  sys.OSArchitecture#系統是32位仍是64位的
18         print sys.NumberOfProcesses #當前系統運行的進程總數
19 
20 def cpu_mem():
21     c = wmi.WMI ()
22     #CPU類型和內存
23     for processor in c.Win32_Processor():
24         #print "Processor ID: %s" % processor.DeviceID
25         print "Process Name: %s" % processor.Name.strip()
26     for Memory in c.Win32_PhysicalMemory():
27         print "Memory Capacity: %.fMB" %(int(Memory.Capacity)/1048576)
28 
29 def cpu_use():
30     #5s取一次CPU的使用率
31     c = wmi.WMI()
32     while True:
33         for cpu in c.Win32_Processor():
34             timestamp = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime())
35             print '%s | Utilization: %s: %d %%' % (timestamp, cpu.DeviceID, cpu.LoadPercentage)
36             time.sleep(5)
37             break
38 
39 def disk():
40     c = wmi.WMI ()
41     #獲取硬盤分區
42     for physical_disk in c.Win32_DiskDrive ():
43         for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"):
44             for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"):
45                 print physical_disk.Caption, partition.Caption, logical_disk.Caption
46 
47     #獲取硬盤使用百分狀況
48     for disk in c.Win32_LogicalDisk (DriveType=3):
49         print disk.Caption, "%0.2f%% free" % (100.0 * long (disk.FreeSpace) / long (disk.Size))
50 
51 def network():
52     c = wmi.WMI ()
53     #獲取MAC和IP地址
54     for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
55         print "MAC: %s" % interface.MACAddress
56     for ip_address in interface.IPAddress:
57         print "ip_add: %s" % ip_address
58     print
59 
60     #獲取自啓動程序的位置
61     for s in c.Win32_StartupCommand ():
62         print "[%s] %s <%s>" % (s.Location, s.Caption, s.Command)
63 
64 
65     #獲取當前運行的進程
66     for process in c.Win32_Process ():
67         print process.ProcessId, process.Name
68 
69 def main():
70     sys_version()
71     cpu_mem()
72     disk()
73     network()
74     #cpu_use()
75 
76 if __name__ == '__main__':
77     main()
78     print platform.system()
79     print platform.release()
80     print platform.version()
81     print platform.platform()
82     print platform.machine()
相關文章
相關標籤/搜索