preface

Recently, I often need to remotely control the computer, such as remotely check the model training situation on the computer, remotely shut down the computer after the model training, etc., so I thought to use Python to simply implement these functions, directly use email remote control of my computer. Let’s have a good time

The development tools

**Python version: **3.6.4

Related modules:

PIL module;

Email module;

And some modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required related modules.

Introduction of the principle

### 1

If not, no operation is performed. If yes, the identity of the sender is determined. If the sender is a legitimate controller, the sender’s mail is parsed and the result is parsed.

### 2. Implementation

(1) Sending and receiving emails

We use POP3 to receive mail and SMTP to send mail. Python has built-in support for both protocols, so it’s easy to implement.

Note 1:

I used sina mailbox on the computer side and QQ mailbox on the mobile phone side.

(2) Check the computer status

The imagegrab.grab () function of PIL module was directly used to take a screenshot of the computer, and then the screenshot was sent to the mobile phone by email.

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

(3) Scalability

In addition to checking the status of the computer, you can only use CMD command to control the computer. If you need to add other functions, you can add them in the following position:

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)Copy the code

(4) Ease of use

CMD command too long mobile phone is inconvenient to type or remember how to do? You can set the shortcut commands in word2cmd.json:

(5) the FAQ

① Failed to send emails on the computer?

Solution:

If the E-mail sent by the computer is identified as spam and the mobile phone cannot check the computer status, you can log in the mailbox used by the computer with the mobile phone and check the returned mail, so that you can check the computer running status on the mobile phone.

Note 3:

If the mobile mailbox is rejected, you can set the whitelist.

② Can’t log in mailbox on computer?

Solution:

You need to enable SMTP and POP3 services:

The password must 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 following configuration files must be modified:

The config. Json.

Function:

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

To encrypt SMTP sessions, set enable_SSL to true and set a correct port number.

Select the following configuration file to modify:

Word2cmd. Json.

Function:

Set the shortcut command.

Step2: run the server. py file

Send instructions:

Execute instructions:

Results:

That concludes this article. Thank you for watching. Follow me every day in my series of Python gadgets

To thank you readers, I’d like to share some of my recent programming favorites to give back to each and every one of you in the hope that they can help you.

Dry goods mainly include:

① Over 2000 Python ebooks (both mainstream and classic books should be available)

②Python Standard Library (Most Complete Chinese version)

③ project source code (forty or fifty interesting and classic practice projects and source code)

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

⑤ A Roadmap for Learning Python

⑥ Two days of Python crawler boot camp live access

All done~ Complete source code + dry plus Python novice learning exchange community: 594356095