一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python實現微信遠程控制電腦

python實現微信遠程控制電腦

2021-01-16 00:43Wlain Python

這篇文章主要為大家詳細介紹了python實現微信遠程控制電腦的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

首先,我們要先看看微信遠程控制電腦的原理是什么呢?

我們可以利用Python的標準庫控制本機電腦,然后要實現遠程的話,我們可以把電子郵件作為遠程控制的渠道,我們用Python自動登錄郵箱檢測郵件,當我們發送關機指令給這個郵箱的時候,若Python檢測到相關的指令,那么Python直接發送本機的相關命令。

下面來分析一下該項目:

1.需求分析

1.范圍:用Python開發一個遠程操控電腦的項目。

2.總體要求:

2.1 總體功能要求:能夠通過該軟件遠程控制該軟件所在的電腦的重啟或關機操作。
2.2 系統要求:開發語言使用Python,并且開發出來的程序能在Windows運行。

2.設計

首先,我們可以利用Python的標準庫控制本機電腦,然后要實現遠程的話,我們可以把電子郵件作為遠程控制的渠道,我們用Python自動登錄郵箱檢測郵件,當我們發送關機指令給這個郵箱的時候,若Python檢測到關機的指令,那么Python直接發送本機的關閉。

3.編寫

本項目的流程圖如下

python實現微信遠程控制電腦

第一步,需要注冊一個新浪郵箱。然后點擊新浪郵箱點擊右上角設置如圖

python實現微信遠程控制電腦

選擇“客戶端pop/imap/smtp”

python實現微信遠程控制電腦

打開新浪郵箱的SMTP與POP3功能

python實現微信遠程控制電腦

具體實現代碼:
配置文件config.ini

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[Slave]
pophost = pop.sina.com
smtphost = smtp.sina.com
port = 25
username = [email protected]
password = XXX
 
[Boss]
timelimit = 2
 
[Command]
shutdown=shutdown -f -s -t 100 -c closing...
dir=dir
 
 
[Open]
music = F:Masetti - Our Own Heaven.mp3
video = F:Jai Waetford - Shy.mp4
notepad = notepad

excutor.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#coding:utf-8
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import os
import win32api
from mccLog import mccLog
 
class executor(object):
 def __init__(self,commandDict,openDict):
  '''
  創建方法
  :param commandDict:
  :param openDict:
  '''
  self.mccLog = mccLog()
  self.commandDict = commandDict
  self.openDict = openDict
 def execute(self,exe,mailHelper):
  self.mailHelper = mailHelper
  subject = exe['subject']
  # self.mccLog.mccWriteLog(u'開始處理命令')
  print u'start to process'
  if subject !='pass':
   self.mailHelper.sendMail('pass','Slave')
   if subject in self.commandDict:
    # self.mccLog.mccWriteLog(u'執行命令!')
    print u'start command'
    try:
     command = self.commandDict[subject]
     os.system(command)
     self.mailHelper.sendMail('Success','Boss')
     # self.mccLog.mccWriteLog(u'執行命令成功!')
     print u'command success'
    except Exception,e:
     # self.mccLog.mccError(u'執行命令失敗'+ str(e))
     print 'command error'
     self.mailHelper.sendMail('error','boss',e)
   elif subject in self.openDict:
    # self.mccLog.mccWriteLog(u'此時打開文件')
    print u'open the file now'
    try:
     openFile = self.openDict[subject]
     win32api.ShellExecute(0,'open',openFile,'','',1)
     self.mailHelper.sendMail('Success','Boss')
     # self.mccLog.mccWriteLog(u'打開文件成功!')
     print u'open file success'
    except Exception,e:
     # self.mccLog.mccError(u'打開文件失敗!' + str(e))
     print u'open file error'
     self.mailHelper.sendMail('error','Boss',e)
   elif subject[:7].lower() =='sandbox':
    self.sandBox(subject[8:])
   else:
    self.mailHelper.sendMail('error','Boss','no such command!')
 
 def sandBox(self,code):
  name = code.split('$n$')[0]
  code = code.split('$n$')[1]
  codestr = '\n'.join(code.split('$c$'))
  codestr = codestr.replace('$',' ')
  with open(name,'a') as f:
   f.write(codestr)
  os.system('python' + name)

configReader.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#-*-coding:utf-8-*-
import ConfigParser
import os,sys
 
class configReader(object):
 def __init__(self,configPath):
  configFile = os.path.join(sys.path[0],configPath)
  self.cReader = ConfigParser.ConfigParser()
  self.cReader.read(configFile)
 
 def readConfig(self,section,item):
  return self.cReader.get(section,item)
 
 def getDict(self,section):
  commandDict = {}#字典
  items = self.cReader.items(section)
  for key,value in items:
   commandDict[key] = value
  return commandDict

日志文件mccLog.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#-*-coding:utf-8-*-
import logging
from datetime import datetime
 
class mccLog(object):
 def __init__(self):
  logging.basicConfig(
   level=logging.DEBUG,
   format='%(asctime)s %(levelname)s %(message)s',
   datefmt='%Y-%m-%d %H:%M:%S',
   filename=datetime. now().strftime('%Y%m%d%H%M%S') + '.log',
   filemode='a'
  )
 
 def mccWriteLog(self,logContent):
   logging.info(logContent)
 
 def mccError(self,errorContent):
   logging.error(errorContent)

mailHelper.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#-*-coding:utf-8-*-
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
from email.mime.text import MIMEText
from configReader import configReader
from mccLog import mccLog
import poplib
import smtplib
import re
 
class mailHelper(object):
 CONFIGPATH = 'config.ini'
 
 def __init__(self):
  '''
  初始化郵件
  '''
  self.mccLog = mccLog()
  cfReader = configReader(self.CONFIGPATH)
  self.pophost = cfReader.readConfig('Slave','pophost')
  self.smtphost = cfReader.readConfig('Slave','smtphost')
  self.port = cfReader.readConfig('Slave','port')
  self.username = cfReader.readConfig('Slave','username')
  self.password = cfReader.readConfig('Slave','password')
  self.bossMail = cfReader.readConfig('Boss','mail')
  self.loginMail()
  self.configSlaveMail()
 
 def loginMail(self):
  '''
  驗證登陸
  :return:
  '''
  self.mccLog.mccWriteLog('start to login the E-mail')
  print 'start to login e-mail'
  try:
   self.pp = poplib.POP3_SSL(self.pophost)
   self.pp.set_debuglevel(0)#可以為0也可以為1,為1時會顯示出來
   self.pp.user(self.username)#復制
   self.pp.pass_(self.password)
   self.pp.list()#列出賦值
   print 'login successful!'
   self.mccLog.mccWriteLog('login the email successful!')
   print 'login the email successful!'
  except Exception,e:
   print 'Login failed!'
   self.mccLog.mccWriteLog('Login the email failed!')
   exit()
 
 def acceptMail(self):
  '''
  接收郵件
  :return:
  '''
  self.mccLog.mccWriteLog('Start crawling mail!')
  print 'Start crawling mail'
  try:
   ret = self.pp.list()
   mailBody = self.pp.retr(len(ret[1]))
   self.mccLog.mccWriteLog('Catch the message successfully')
   print 'Catch the message successfully'
   return mailBody
  except Exception,e:
   self.mccLog.mccError('Catch the message failed' + e)
   print 'Catch the message failed'
   return None
 
 def analysisMail(self,mailBody):
  '''
  正則分析郵件
  :param mailBody:
  :return:
  '''
  self.mccLog.mccWriteLog('Start crawling subject and sender')
  print 'Start crawling subject and sender'
  try:
   subject = re.search("Subject: (.*?)',",str(mailBody[1]).decode('utf-8'),re.S).group(1)
   print subject
   sender = re.search("'X-Sender: (.*?)',",str(mailBody[1]).decode('utf-8'),re.S).group(1)
   command = {'subject':subject,'sender':sender}
   self.mccLog.mccWriteLog("crawling subject and sender successful!")
   print 'crawling subject and sender successful'
   return command
  except Exception,e:
   self.mccLog.mccError("crawling subject and sender failed!" + e)
   print 'crawling subject and sender failed!'
   return None
 
 def sendMail(self,subject,receiver,body='Success'):
  '''
  發送郵件
  :param subject:
  :param receiver:
  :param body:
  :return:
  '''
  msg = MIMEText(body,'plain','utf-8')
  #中文需要參數utf-8,單字節字符不需要
  msg['Subject'] = subject
  msg['from'] = self.username
  self.mccLog.mccWriteLog('Start sending mail' + 'to' +receiver)
  print 'Start sending mail'
  if receiver == 'Slave':
   try:
    self.handle.sendmail(self.username,self.username,msg.as_string())
    self.mccLog.mccWriteLog('Send the message successfully')
    print 'Send the message successfully'
   except Exception,e:
    self.mccLog.mccError('Send the message failed' + e)
    print 'Send the message failed'
    return False
  elif receiver == 'Boss':
   try:
    self.handle.sendmail(self.username,self.bossMail,msg.as_string())
    self.mccLog.mccWriteLog('Send the message successfully')
    print 'Send the message successfully'
   except Exception,e:
    self.mccLog.mccError('Send the message failed!' + e)
    print 'Send the message failed!'
    return False
 
 def configSlaveMail(self):
  '''
  配置郵件
  :return:
  '''
  self.mccLog.mccWriteLog('Start configuring the mailbox')
  print 'Start configuring the mailbox'
  try:
   self.handle = smtplib.SMTP(self.smtphost, self.port)
   self.handle.login(self.username, self.password)
   self.mccLog.mccWriteLog('The mailbox configuration is successful')
   print 'The mailbox configuration is successful'
  except Exception, e:
   self.mccLog.mccError('The mailbox configuration is failed' + e)
   print 'The mailbox configuration is failed'
   exit()
 
#
# if __name__=='__main__':
#  mail = mailHelper()
#  body = mail.acceptMail()
#  print body
#  print mail.analysisMail(body)
#  mail.sendMail('OK','Slave')

weiChatControlComputer.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#-*-coding:utf-8-*-
 
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import time
import sys
from mailHelper import mailHelper
from excutor import executor
from configReader import configReader
 
__Author__ = 'william'
__Verson__ = 0.5
 
reload(sys)
sys.setdefaultencoding('utf-8')
 
class MCC(object):
 CONFIGPATH = 'config.ini'
 KEY_COMMAND = 'Command'
 KEY_OPEN = 'Open'
 KEY_BOSS = 'Boss'
 KEY_TIMELIMIT = 'timelimit'#掃描時間的頻率
 
 def __init__(self):
  self.mailHelper = mailHelper()
  self.configReader = configReader(self.CONFIGPATH)
  commandDict = self.configReader.getDict(self.KEY_COMMAND)
  openDict = self.configReader.getDict(self.KEY_OPEN)
  self.timeLimit = int(self.configReader.readConfig(self.KEY_BOSS,self.KEY_TIMELIMIT))
  self.excutor = executor(commandDict,openDict)
  self.toRun()
 
 def toRun(self):
  '''
  實現輪訓操作
  :return:
  '''
  while True:
   self.mailHelper = mailHelper()
   self.run()
   time.sleep(self.timeLimit)
 
 def run(self):
  mailBody = self.mailHelper.acceptMail()
  if mailBody:
   exe = self.mailHelper.analysisMail(mailBody)
   if exe:
    self.excutor.execute(exe,self.mailHelper)
 
 
if __name__ == '__main__':
 mcc = MCC()

運行截圖:

python實現微信遠程控制電腦

4.總結

在這個小項目的編寫過程中,知道了項目開發的基本流程并且走了一遍,通過項目管理的方式去開發項目,并且在這個小項目開發的過程中,復習了Python一些初級階段的基礎知識,并且更深刻體會到從項目的設計到項目的實施,以及項目的測試運維等步驟需要程序員深刻的理解,這樣才能在項目中逐漸完善自我。

待續。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/qq_30070433/article/details/73731187

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产九九视频在线观看 | kuaibo成人播放器 | 国产在线观看一区 | 国产午夜精品不卡视频 | 欧美日韩精品乱国产 | 色聚网久久综合 | 欧美日韩综合网在线观看 | 天天操天天干天天做 | 国产美女屁股直流白浆视频无遮挡 | 51国产午夜精品免费视频 | 摄像头东北对白清晰 | 日韩去日本高清在线 | fuqer老师 | 国产高清自拍视频 | 欧美xxxxxbb | 婷综合| 青青久久久国产线免观 | 国产午夜大片 | 女人爽到喷水的视频免费看 | 亚洲 欧美 日韩 国产 视频 | 精品99一区二区三区麻豆 | 国产成人免费观看在线视频 | 91在线精品国产丝袜超清 | 深夜激情网 | 大桥未久midd—962在线 | 日本阿v精品视频在线观看 日本xxx片免费高清在线 | 97国产精品久久碰碰牛牛 | chinese男同志同性twink | 秋霞综合网 | 成人高清网站 | a级精品九九九大片免费看 a级动漫 | 成年人免费看的视频 | 国语视频高清在线观看 | 日麻逼| 99视频九九精品视频在线观看 | 日韩精品成人免费观看 | 亚洲高清视频免费 | 久久精品成人免费看 | 国产未成女年一区二区 | 蜜桃视频在线观看www | 欧美高清milf在线播放 |