preface

Recently, I often need to simply control the computer remotely, such as checking the model training on the computer remotely, and shutting down the computer remotely after the model training, etc. Therefore, I want to use Python to simply realize these functions, and directly control my computer remotely by email. Let’s get started happily

The development tools

Python version: 3.6.4

Related modules:

PIL module;

Email module;

And some of the modules that come with Python.

Environment set up

Install Python and add it to the environment variable, and the PIP will install the appropriate module.

Introduction of the principle

A train of thought.

Every once in a while, the computer checks whether a new mail has been received in the specified mailbox. If it has not been received, it does not conduct any operation. If it has been received, it judges the identity of the sender.

Two. Specific implementation

(1) Send and receive emails

We use the POP3 protocol for receiving mail and the SMTP protocol for sending mail, and Python’s built-in support for both protocols makes it easy to implement.

Note 1:

I used Sina mailbox for computer terminal mailbox, and QQ mailbox for mobile phone terminal mailbox.

(2) Check the computer status

Use the imageGrab. Grab () function of the PIL module directly to take the screen shot of the computer, and then send the screen shot of the computer to the mobile phone by email.

If you want to see the model in action, just display the model training interface on your desktop before you leave

(3) Extensibility

In addition to the status of the computer, it is currently only supported to control the computer with the CMD command. If you need to add other functions, you can add them in the position shown in the following figure:

def run(self): print('[INFO]:Start server successfully... ') while True: self.email.reset_pop() mails = self.email.get('list')['list'][1] if len(mails) > self.num_msg: for i in range(self.num_msg+1, len(mails)+1): res = self.email.get(i) res_from = res[i]['From'] res_from = re.findall(r'<(.*?) >', res_from)[0].lower() print(res_from) if res_from ! = options['sender']['email'].lower(): continue command = res[i]['Subject'] if command in word2cmd_dict: command = word2cmd_dict[command] if command == 'screenshot': savename = './screenshot.jpg' self.screenshot(savename) try: is_success = self.email.send(attach_path=savename) if not is_success: raise RuntimeError('Fail to send screenshot... ') print('[INFO]: Send screenshot successfully... ') except: print('[Error]: Fail to send screenshot... ') else: self.run_cmd(command) self.num_msg = len(mails) time.sleep(self.time_interval)

(4) Ease of use

CMD command is too long mobile phone is not convenient or can not remember how to do? You can set a shortcut in word2cmd.json:

(5) the FAQ

① The computer failed to send mail?

Solution:

If the email sent by the computer side is identified as spam and the mobile side cannot check the status of the computer, you can log in the email box used by the computer side with your mobile phone and check the returned email, so you can check the status of the computer on your mobile phone.

Note 3:

If the mobile terminal mailbox refused to receive, you can set a whitelist.

② The computer terminal can not log in the mailbox?

Solution:

You will need to enable SMTP and POP3 services:

The password should be the client authorization password, not the login password. Of course, the login password of Sina mailbox is the client authorization password.

Using the demonstration

Step1: Modify the configuration file

The configuration files that must be modified are:

The config. Json.

Function:

Set the information of the computer terminal and the mobile terminal mailbox. The information of the mobile terminal mailbox is used to verify whether the sender is legally controlling the computer, and the information of the computer terminal mailbox is used to receive and send emails on the computer terminal.

If you need to encrypt an SMTP session, set ENABLE_SSL to true and set the correct port number.

Select the modified configuration file as:

Word2cmd. Json.

Function:

Set the shortcut command.

Step2: Run the server.py file

Send instruction:

Execute instructions:

Results:

That’s it for now. Thanks for watching. Follow me for my daily series of Python gadgets

To thank you readers, I’d like to share with you some of my recent collection of dry programming goodies and give something back to every reader in the hope of helping you out.

Click to get dry goods mainly:

① More than 2000 Python e-books (mainstream and classic books are available)

(2) The Python Standard Library (Chinese version)

③ project source code (40 or 50 interesting and classic hands-on project and source code)

④Python basic introduction, crawler, web development, big data analysis video (suitable for small white learning)

⑤Python Learning Roadmap (Goodbye to Slow Learning)

⑥ Two days of Python crawler training camp live access

All done~ complete source code + dry goods see personal profile or private message to obtain the relevant files.