python實(shí)現(xiàn)微信遠(yuǎn)程控制電腦
首先,我們要先看看微信遠(yuǎn)程控制電腦的原理是什么呢?
我們可以利用Python的標(biāo)準(zhǔn)庫(kù)控制本機(jī)電腦,然后要實(shí)現(xiàn)遠(yuǎn)程的話,我們可以把電子郵件作為遠(yuǎn)程控制的渠道,我們用Python自動(dòng)登錄郵箱檢測(cè)郵件,當(dāng)我們發(fā)送關(guān)機(jī)指令給這個(gè)郵箱的時(shí)候,若Python檢測(cè)到相關(guān)的指令,那么Python直接發(fā)送本機(jī)的相關(guān)命令。
下面來(lái)分析一下該項(xiàng)目:
1.需求分析
1.范圍:用Python開發(fā)一個(gè)遠(yuǎn)程操控電腦的項(xiàng)目。
2.總體要求:
2.1 總體功能要求:能夠通過(guò)該軟件遠(yuǎn)程控制該軟件所在的電腦的重啟或關(guān)機(jī)操作。
2.2 系統(tǒng)要求:開發(fā)語(yǔ)言使用Python,并且開發(fā)出來(lái)的程序能在Windows運(yùn)行。
2.設(shè)計(jì)
首先,我們可以利用Python的標(biāo)準(zhǔn)庫(kù)控制本機(jī)電腦,然后要實(shí)現(xiàn)遠(yuǎn)程的話,我們可以把電子郵件作為遠(yuǎn)程控制的渠道,我們用Python自動(dòng)登錄郵箱檢測(cè)郵件,當(dāng)我們發(fā)送關(guān)機(jī)指令給這個(gè)郵箱的時(shí)候,若Python檢測(cè)到關(guān)機(jī)的指令,那么Python直接發(fā)送本機(jī)的關(guān)閉。
3.編寫
本項(xiàng)目的流程圖如下
第一步,需要注冊(cè)一個(gè)新浪郵箱。然后點(diǎn)擊新浪郵箱點(diǎn)擊右上角設(shè)置如圖
選擇“客戶端pop/imap/smtp”
打開新浪郵箱的SMTP與POP3功能
具體實(shí)現(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'此時(shí)打開文件') 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): ''' 驗(yàn)證登陸 :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時(shí)會(huì)顯示出來(lái) 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'#掃描時(shí)間的頻率 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): ''' 實(shí)現(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()
運(yùn)行截圖:
4.總結(jié)
在這個(gè)小項(xiàng)目的編寫過(guò)程中,知道了項(xiàng)目開發(fā)的基本流程并且走了一遍,通過(guò)項(xiàng)目管理的方式去開發(fā)項(xiàng)目,并且在這個(gè)小項(xiàng)目開發(fā)的過(guò)程中,復(fù)習(xí)了Python一些初級(jí)階段的基礎(chǔ)知識(shí),并且更深刻體會(huì)到從項(xiàng)目的設(shè)計(jì)到項(xiàng)目的實(shí)施,以及項(xiàng)目的測(cè)試運(yùn)維等步驟需要程序員深刻的理解,這樣才能在項(xiàng)目中逐漸完善自我。
待續(xù)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)遠(yuǎn)程控制電腦
- python得到電腦的開機(jī)時(shí)間方法
- python實(shí)現(xiàn)Windows電腦定時(shí)關(guān)機(jī)
- Python3.4實(shí)現(xiàn)遠(yuǎn)程控制電腦開關(guān)機(jī)
- windows下python模擬鼠標(biāo)點(diǎn)擊和鍵盤輸示例
- Python中使用PyHook監(jiān)聽鼠標(biāo)和鍵盤事件實(shí)例
- 利用Python實(shí)現(xiàn)Windows下的鼠標(biāo)鍵盤模擬的實(shí)例代碼
- python之模擬鼠標(biāo)鍵盤動(dòng)作具體實(shí)現(xiàn)
- Python使用pyautogui模塊實(shí)現(xiàn)自動(dòng)化鼠標(biāo)和鍵盤操作示例
- python實(shí)現(xiàn)鍵盤控制鼠標(biāo)移動(dòng)
- 利用webqq協(xié)議使用python登錄qq發(fā)消息源碼參考
- python實(shí)現(xiàn)控制電腦鼠標(biāo)和鍵盤,登錄QQ的方法示例
相關(guān)文章
python機(jī)器學(xué)習(xí)基礎(chǔ)K近鄰算法詳解KNN
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)基礎(chǔ)K近鄰算法詳解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-11-11Python?watchdog靈活監(jiān)控文件和目錄的變化
Python?Watchdog是一個(gè)強(qiáng)大的Python庫(kù),它提供了簡(jiǎn)單而靈活的方式來(lái)監(jiān)控文件系統(tǒng)的變化,本文將詳細(xì)介紹Python?Watchdog的用法和功能,包括安裝、基本用法、事件處理以及實(shí)際應(yīng)用場(chǎng)景,并提供豐富的示例代碼2024-01-01跟老齊學(xué)Python之一個(gè)免費(fèi)的實(shí)驗(yàn)室
學(xué)習(xí)Python也要做實(shí)驗(yàn),也就是嘗試性地看看某個(gè)命令到底什么含義。在《集成開發(fā)環(huán)境(IDE)》一章中,我們介紹了Python的IDE時(shí),給大家推薦了IDLE,進(jìn)入到IDLE中,看到>>>符號(hào),可以在后面輸入一行指令。其實(shí),這就是一個(gè)非常好的實(shí)驗(yàn)室。2014-09-09Python多項(xiàng)式回歸的實(shí)現(xiàn)方法
這篇文章主要介紹了Python多項(xiàng)式回歸的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03Python實(shí)現(xiàn)線性判別分析(LDA)的MATLAB方式
今天小編大家分享一篇Python實(shí)現(xiàn)線性判別分析(LDA)的MATLAB方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python中對(duì)象的比較操作==和is區(qū)別詳析
這篇文章主要給大家介紹了關(guān)于Python中對(duì)象的比較操作==和is區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02