Python Method of Monitoring and Controlling Computer by Mobile Phone
- 2021-11-02 01:36:11
- OfStack
1. Preface
Most of the time, we have the need to control computers remotely. For example, if you are downloading something, you need to shut down your computer after downloading it. Or you need to monitor the running status of a program.
Today, we will use Python to realize a small program for remote monitoring and controlling computers.
2. Implementation principle
It sounds very advanced to control computers remotely, but it is actually very simple to implement. The implementation principle is as follows:
Run the program and let the program keep reading the mail Send email to computer by mobile phone Judge whether the mail with the specified subject is read or not, and if so, obtain the mail content Execute the default function according to the message contentIt's not so much learning how to control computers remotely as learning how to read emails. Of course, the above process only realizes remote control of the computer, but does not realize the monitoring of the computer. The monitoring operation can be carried out in the form of screenshots.
We can preset a command, and when we read the email content as grab, we will send a screenshot of the computer. How to send computer screenshots to mobile phone mailbox, so as to achieve the monitoring effect.
Please refer to the blog about how to send mail: How to send mail with Python? . I won't go into details here. Let's take a look at how to read mail.
STEP 3 Read mail
imbox module is needed to read mail, and the installation statement is as follows:
pip install imbox
The code to read the message is as follows:
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 it is a remote control mail sent by the mobile phone,
if message.subject == 'Remote Control':
# Mark as read
box.mark_seen(uid)
return message.body['plain'][0]
First, we use with statement to open the mailbox. Then get all unread messages by the following statement:
all_msg = box.messages(unread=True)
After getting unread messages, traverse the messages. Mark messages with the subject "Reomte Control" as read and return text content.
It should be noted here that because we have screened out emails with the theme of "Remote Control", we need to set the theme to "Remote Control" when sending emails on our mobile phones, so as to avoid the interference of other emails.
4. Screenshot
Screenshots need to use PIL module, which is installed as follows:
pip install pillow
The code for the screenshot is simple:
from PIL import ImageGrab
def grab(sender, to):
# Intercept the full screen of the computer
surface = ImageGrab.grab()
# Save screenshots as surface.jpg
surface.save('surface.jpg')
# Send screenshots to mobile phones
send_mail(sender, to, ['surface.jpg'])
The code for send_mail is as follows:
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)
For an introduction to sending emails, please refer to the blog mentioned above.
Step 5 Shut down
Shutdown operation is very simple, we can use python to execute command line statements. The code is as follows:
import os
def shutdown():
# Shut down
os.system('shutdown -s -t 0')
In addition to shutting down, we can also perform many operations. For 1 complex operation, we can prewrite 1 bat files, which will not be demonstrated here.
6. Complete code
Above, we wrote the code of each part, and then we will look at the code of the main part:
def main():
# A mailbox used by a computer to send mail that has been read by the computer
username = 'sockwz@163.com'
password = '********'
# Mailbox on mobile phone
receiver = '2930777518@qq.com'
# Time interval for reading messages
time_space = 5
# Registered account
yagmail.register(username, password)
# Cyclic reading
while True:
# Read unread messages
msg = read_mail(username, password)
if msg:
# Perform different actions according to different contents
if msg == 'shutdown':
shutdown()
elif msg == 'grab':
grab(username, receiver)
time.sleep(time_space)
We can write some other functions according to our own needs. Here's the complete code:
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 it is a remote control mail sent by the mobile phone,
if message.subject == 'Remote Control':
# Mark as read
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 = ' Your authorization code '
receiver = '2930777518@qq.com'
time_space = 5
yagmail.register(username, password)
while True:
# Read unread messages
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()