不少时候,咱们都有远程控制电脑的需求。好比正在下载某样东西,须要让电脑在下载完后关机。或者你须要监控一个程序的运行情况等。python
今天咱们就来用Python实现一个远程监控并控制电脑的小程序。小程序
听起来远程控制电脑好像很高级的样子,可是实现起来其实很是简单。实现原理以下:markdown
与其说是学习如何远程控制电脑,还不如说是学习如何读取邮件。固然,上面的的流程只实现了远程控制电脑,而没实现对电脑的监控。而监控的操做能够以截图的方式来进行。函数
咱们能够预设一个指令,当读取到邮件内容为grab时,咱们就发送电脑截图。如何将电脑截图发送给手机邮箱,这样就达到了监控的效果。oop
关于如何发送邮件能够参考博客:如何用Python发送邮件?。这里就再也不详细说了。下面咱们看看如何读取邮件。学习
读取邮件须要使用到imbox模块,安装语句以下:ui
pip install imbox
复制代码
读取邮件的代码以下:spa
from imbox import Imbox
def read_mail(username, password):
with Imbox('imap.163.com', username, password, ssl=True) as box:
all_msg = box.messages(unread=True)
for uid, message in all_msg:
# 若是是手机端发来的远程控制邮件
if message.subject == 'Remote Control':
# 标记为已读
box.mark_seen(uid)
return message.body['plain'][0]
复制代码
首先咱们用with语句,打开邮箱。而后经过下面语句获取全部的未读邮件:.net
all_msg = box.messages(unread=True)
复制代码
获取未读邮件后,对邮件进行遍历。将主题为“Reomte Control”的邮件标记为已读,并返回文本内容。命令行
这里须要注意,由于咱们筛选出了主题为“Remote Control”的邮件,所以咱们在用手机发邮件的时候须要将主题设置为“Remote Control”,这样能够避免其它邮件的干扰。
截图须要使用到PIL模块,安装以下:
pip install pillow
复制代码
截图的代码很简单:
from PIL import ImageGrab
def grab(sender, to):
# 截取电脑全屏
surface = ImageGrab.grab()
# 将截屏保存为surface.jpg
surface.save('surface.jpg')
# 将截屏发送给手机
send_mail(sender, to, ['surface.jpg'])
复制代码
其中send_mail的代码以下:
import yagmail
def send_mail(sender, to, contents):
smtp = yagmail.SMTP(user=sender, host='smtp.163.com')
smtp.send(to, subject='Remote Control', contents=contents)
复制代码
关于发送邮件的介绍能够参考上面提到的博客。
关机的操做很是简单,咱们能够用python来执行命令行语句便可。代码以下:
import os
def shutdown():
# 关机
os.system('shutdown -s -t 0')
复制代码
除了关机,咱们还能够执行不少操做。对于一些复杂的操做,咱们能够预编写一些bat文件,这里就不演示了。
上面咱们编写了各个部分的代码,而后再来看看主体部分的代码:
def main():
# 电脑用来发送邮件已经电脑读取的邮箱
username = 'sockwz@163.com'
password = '********'
# 手机端的邮箱
receiver = '2930777518@qq.com'
# 读取邮件的时间间隔
time_space = 5
# 注册帐户
yagmail.register(username, password)
# 循环读取
while True:
# 读取未读邮件
msg = read_mail(username, password)
if msg:
# 根据不一样的内容执行不一样操做
if msg == 'shutdown':
shutdown()
elif msg == 'grab':
grab(username, receiver)
time.sleep(time_space)
复制代码
其中:
yagmail.register(username, password)
复制代码
会使用到keyring
模块,安装以下:
pip install keyring
复制代码
后面咱们能够根据本身的需求编写一些其它功能。下面是完整的代码:
import os
import time
import yagmail
from imbox import Imbox
from PIL import ImageGrab
def send_mail(sender, to, contents):
smtp = yagmail.SMTP(user=sender, host='smtp.163.com')
smtp.send(to, subject='Remote Control', contents=contents)
def read_mail(username, password):
with Imbox('imap.163.com', username, password, ssl=True) as box:
all_msg = box.messages(unread=True)
for uid, message in all_msg:
# 若是是手机端发来的远程控制邮件
if message.subject == 'Remote Control':
# 标记为已读
box.mark_seen(uid)
return message.body['plain'][0]
def shutdown():
os.system('shutdown -s -t 0')
def grab(sender, to):
surface = ImageGrab.grab()
surface.save('surface.jpg')
send_mail(sender, to, ['surface.jpg'])
def main():
username = 'sockwz@163.com'
password = '你的受权码'
receiver = '2930777518@qq.com'
time_space = 5
yagmail.register(username, password)
while True:
# 读取未读邮件
msg = read_mail(username, password)
if msg:
if msg == 'shutdown':
shutdown()
elif msg == 'grab':
grab(username, receiver)
time.sleep(time_space)
if __name__ == '__main__':
main()
复制代码