One, foreword

Most of the time, we have the need to control our computers remotely. For example, if you are downloading something, you need to shut down the computer after downloading it. Or you need to monitor the health of an application, etc.

Today we are going to use Python to implement a remote monitoring and control computer small program.

Two, the implementation principle

Remotely controlling a computer may sound sophisticated, but it’s actually quite simple to implement. The implementation principle is as follows:

  1. Run the program, let the program constantly read mail

  2. Send emails to your computer from your phone

  3. Determines whether an email with the specified subject has been read. If so, the email content is obtained

  4. Execute the default function based on the message content

It’s not so much learning how to remotely control a computer as learning how to read email. Of course, the above process only realizes the remote control of the computer, but does not realize the monitoring of the computer. And the operation of monitoring can be carried out in the way of screenshot.

We can set up a command and when we read an email as grab, we send a screenshot of the computer. How to send computer screenshots to mobile email, so as to achieve the effect of monitoring.

For information on how to send emails, see the blog: How to Send Emails in Python? . I won’t go into details here. Let’s look at how to read messages.

3. Read mail

The imbox module is required to read mail, and the installation statement is as follows:

pip install imbox
Copy the code

The code to read the message is as follows:

from imbox import Imboxdef 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': # Mark as read box. Mark_seen (uid) return message.body['plain'][0]Copy the code

First we open the mailbox with the with statement. Then get all unread messages with the following statement:

all_msg = box.messages(unread=True)
Copy the code

After obtaining unread messages, the messages are traversed. Marks messages with the subject “Reomte Control” as read and returns the text content.

We need to pay attention here, because we have screened out the emails with the subject of “Remote Control”, so we need to set the subject to “Remote Control” when we send emails with mobile phones, so as to avoid the interference of other emails.

Four, screenshots

PIL module is required for screenshots, and the installation is as follows:

pip install pillow
Copy the code

The code for the screenshot is simple:

from PIL import ImageGrabdef grab(sender, to): JPG surf.save (' surf.jpg ') # Send_mail (sender, to, ['surface.jpg'])Copy the code

The code of send_mail is as follows:

import yagmaildef send_mail(sender, to, contents):
    smtp = yagmail.SMTP(user=sender, host='smtp.163.com')    smtp.send(to, subject='Remote Control', contents=contents)
Copy the code

For an introduction to emailing, see the blog mentioned above.

Fifth, to turn it off

Shutdown operations are very simple, we can use Python to execute command line statements. The code is as follows:

Import osdef shutdown(): # shutdown os.system('shutdown -s -t 0')Copy the code

We can perform many operations besides shutting down. For some complex operations, we can prewrite some BAT files, which will not be demonstrated here.

Vi. Complete code

We wrote the code for each part above, and then we’ll look at the code for the body part:

def main(): Username = '[email protected]' password = '********' # mobile receiver = '[email protected]' # Time_space = 5 # register(username, password) # loop while True: If MSG == 'shutdown' if MSG == 'shutdown': shutdown() elif msg == 'grab': grab(username, receiver) time.sleep(time_space)Copy the code

Among them:

yagmail.register(username, password)
Copy the code

The keyring module will be used and installed as follows:

pip install keyring
Copy the code

Later we can write some additional functionality to suit our needs. Here is the complete code:

import osimport timeimport yagmailfrom imbox import Imboxfrom 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': Text.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 = '[email protected]' password = 'your license code' receiver = '[email protected]' time_space = 5 yagmail.register(username, MSG = read_mail(username, password) if MSG == 'shutdown': shutdown() elif msg == 'grab': grab(username, receiver) time.sleep(time_space)if __name__ == '__main__': main()Copy the code

Recently, many friends have sent messages to ask about learning Python. For easy communication, click on blue to join yourselfDiscussion solution resource base