设为首页 收藏本站
查看: 1357|回复: 0

[经验分享] 关于Python 获取windows信息收集

[复制链接]

尚未签到

发表于 2015-12-1 12:25:26 | 显示全部楼层 |阅读模式
  
  收集一些Python操作windows的代码 (不管是自带的or第三方库)均来自网上
  1.shutdown 操作
  定时关机、重启、注销



#!/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_())
DSC0000.png
  
  2.windows锁屏
  相当与WIN+L 操作



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_())
DSC0001.png
  
  3.获取电脑的基本信息
  引用第三方库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()
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-145857-1-1.html 上篇帖子: Python 基础学习 总结篇 下篇帖子: 关于python中的setup.py
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表