最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實現(xiàn)微信遠程控制電腦

 更新時間:2018年02月22日 14:11:45   作者:Wlain  
這篇文章主要為大家詳細介紹了python實現(xiàn)微信遠程控制電腦的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

下面來分析一下該項目:

1.需求分析

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

2.總體要求:

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

2.設(shè)計

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

3.編寫

本項目的流程圖如下

 

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

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

 

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

具體實現(xiàn)代碼:
配置文件config.ini

[Slave]
pophost = pop.sina.com
smtphost = smtp.sina.com
port = 25
username = XXX@sina.com
password = XXX

[Boss]
mail = XXX@qq.com
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

#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):
  '''
  創(chuàng)建方法
  :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'執(zhí)行命令!')
    print u'start command'
    try:
     command = self.commandDict[subject]
     os.system(command)
     self.mailHelper.sendMail('Success','Boss')
     # self.mccLog.mccWriteLog(u'執(zhí)行命令成功!')
     print u'command success'
    except Exception,e:
     # self.mccLog.mccError(u'執(zhí)行命令失敗'+ 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

#-*-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

#-*-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

#-*-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)#復(fù)制
   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'):
  '''
  發(fā)送郵件
  :param subject:
  :param receiver:
  :param body:
  :return:
  '''
  msg = MIMEText(body,'plain','utf-8')
  #中文需要參數(shù)utf-8,單字節(jié)字符不需要
  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

#-*-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):
  '''
  實現(xiàn)輪訓(xùn)操作
  :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()

運行截圖:

4.總結(jié)

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

待續(xù)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python并發(fā)編程線程消息通信機制詳解

    Python并發(fā)編程線程消息通信機制詳解

    這篇文章主要為大家介紹了Python并發(fā)編程之線程消息通信機制的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-10-10
  • python eventlet綠化和patch原理

    python eventlet綠化和patch原理

    這篇文章主要介紹了python eventlet綠化和patch原理,幫助大家更好的理解和學(xué)習(xí)python eventlet工具的使用,感興趣的朋友可以了解下
    2020-11-11
  • Python使用dict.fromkeys()快速生成一個字典示例

    Python使用dict.fromkeys()快速生成一個字典示例

    這篇文章主要介紹了Python使用dict.fromkeys()快速生成一個字典,結(jié)合實例形式分析了Python基于dict.fromkeys()生成字典的相關(guān)操作技巧,需要的朋友可以參考下
    2019-04-04
  • Python中ImportError錯誤的詳細解決方法

    Python中ImportError錯誤的詳細解決方法

    最近辛辛苦苦安裝完了python,最后再運行的時候會出現(xiàn)錯誤,所以這篇文章主要給大家介紹了關(guān)于Python中ImportError錯誤的詳細解決方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-07-07
  • Python跨文件全局變量的使用技巧

    Python跨文件全局變量的使用技巧

    Python?中?global?關(guān)鍵字可以定義一個變量為全局變量,但是這個僅限于在一個模塊(py文件)中調(diào)用全局變量,在另外一個py文件?再次使用?global?x?也是無法訪問到的,這篇文章主要介紹了Python跨文件全局變量的使用,需要的朋友可以參考下
    2022-01-01
  • Python實現(xiàn)softmax反向傳播的示例代碼

    Python實現(xiàn)softmax反向傳播的示例代碼

    這篇文章主要為大家詳細介紹了Python實現(xiàn)softmax反向傳播的相關(guān)資料,文中的示例代碼講解詳細,具有一定的參考價值,感興趣的可以了解一下
    2023-04-04
  • 編程小妙招:Python帶你玩轉(zhuǎn)Excel超鏈接

    編程小妙招:Python帶你玩轉(zhuǎn)Excel超鏈接

    掌握Python實現(xiàn)Excel加超鏈接的技巧,讓你的數(shù)據(jù)報告活起來,本指南將帶你輕松穿梭于單元格間,一行代碼搞定鏈接,別等了,跟我一起讓你的Excel工作表不僅聰明,還能“點”亮你的信息網(wǎng)絡(luò)!
    2023-12-12
  • Python數(shù)據(jù)結(jié)構(gòu)之樹的全面解讀

    Python數(shù)據(jù)結(jié)構(gòu)之樹的全面解讀

    數(shù)據(jù)結(jié)構(gòu)中有很多樹的結(jié)構(gòu),其中包括二叉樹、二叉搜索樹、2-3樹、紅黑樹等等。本文中對數(shù)據(jù)結(jié)構(gòu)中常見的樹邏輯結(jié)構(gòu)和存儲結(jié)構(gòu)進行了匯總,不求嚴格精準,但求簡單易懂
    2021-11-11
  • Pycharm2020.1安裝無法啟動問題即設(shè)置中文插件的方法

    Pycharm2020.1安裝無法啟動問題即設(shè)置中文插件的方法

    這篇文章主要介紹了Pycharm2020.1安裝無法啟動問題即設(shè)置中文插件的操作方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-08-08
  • Python EOL while scanning string literal問題解決方法

    Python EOL while scanning string literal問題解決方法

    這篇文章主要介紹了Python EOL while scanning string literal問題解決方法,本文總結(jié)出是數(shù)據(jù)庫數(shù)據(jù)出現(xiàn)問題導(dǎo)致這個問題,需要的朋友可以參考下
    2015-04-04

最新評論

平顶山市| 安多县| 揭东县| 龙南县| 宾阳县| 四会市| 三明市| 文化| 肇州县| 成安县| 华坪县| 南丹县| 东港市| 阜阳市| 大足县| 大渡口区| 阳城县| 邵武市| 揭阳市| 敦煌市| 扬州市| 江口县| 曲水县| 沁源县| 美姑县| 华亭县| 定远县| 长春市| 尚义县| 南召县| 民和| 祥云县| 武功县| 乐东| 米泉市| 容城县| 潞西市| 翁牛特旗| 瓮安县| 宁陵县| 弥勒县|