Python实现用手机监控远程控制电脑( 二 )

其中:
yagmail.register(username, password)会使用到keyring模块,安装如下:
pip install keyring后面我们可以根据自己的需求编写一些其它功能 。下面是完整的代码:
import osimport timeimport yagmailfrom imbox import Imboxfrom PIL import ImageGrabdef 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()
【Python实现用手机监控远程控制电脑】


推荐阅读