来源:https://zacksock.blog.csdn.net/
一、前言
二、实现原理
与其说是学习如何远程控制电脑,还不如说是学习如何读取邮件。当然,上面的的流程只实现了远程控制电脑,而没实现对电脑的监控。而监控的操作可以以截图的方式来进行。
三、读取邮件
pip install imbox
读取邮件的代码如下:
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语句,打开邮箱。然后通过下面语句获取所有的未读邮件:
all_msg = box.messages(unread=True)
四、截图
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)
五、关机
import os
def shutdown():
# 关机
os.system('shutdown -s -t 0')
六、完整代码
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)
我们可以根据自己的需求编写一些其它功能。下面是完整的代码:
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()
如果觉得我的分享不错,欢迎大家随手点赞。
PS:欢迎在留言区留下你的观点,一起讨论提高。如果今天的文章让你有新的启发,欢迎转发分享给更多人。
Python读者交流群已成立
公众号运营至今,离不开小伙伴们的支持。为了给小伙伴们提供一个互相交流的平台,特地开通了官方交流群。扫描下方二维码备注 进群 或者关注公众号 Python人工智能编程 后获取进群通道。
添加好友,备注【进群】
—————END—————
推荐阅读:
最近面试BAT,整理一份Python资料《Python学习手册》,覆盖了Python知识点、人工智能、深度学习、机器学习等方面。
获取方式:关注公众号并回复 Python 领取,更多内容陆续奉上。
明天见(。・ω・。)ノ♡
原创文章,作者:栈长,如若转载,请注明出处:https://www.cxyquan.com/11439.html