一、前言
很多時候,我們都有遠程控制電腦的需求。比如正在下載某樣東西,需要讓電腦在下載完后關機。或者你需要監控一個程序的運行狀況等。
今天我們就來用Python實現一個遠程監控并控制電腦的小程序。
二、實現原理
聽起來遠程控制電腦好像很高級的樣子,但是實現起來其實非常簡單。實現原理如下:
- 運行程序,讓程序不停地讀取郵件
- 用手機給電腦發送郵件
- 判斷是否讀取到指定主題的郵件,如果有,則獲取郵件內容
- 根據郵件內容,執行預設的函數
與其說是學習如何遠程控制電腦,還不如說是學習如何讀取郵件。當然,上面的的流程只實現了遠程控制電腦,而沒實現對電腦的監控。而監控的操作可以以截圖的方式來進行。
我們可以預設一個指令,當讀取到郵件內容為grab時,我們就發送電腦截圖。如何將電腦截圖發送給手機郵箱,這樣就達到了監控的效果。
關于如何發送郵件可以參考博客:如何用Python發送郵件?。這里就不再詳細說了。下面我們看看如何讀取郵件。
三、讀取郵件
讀取郵件需要使用到imbox模塊,安裝語句如下:
1
|
pip install imbox |
讀取郵件的代碼如下:
1
2
3
4
5
6
7
8
9
10
11
|
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 message.subject = = 'Remote Control' : # 標記為已讀 box.mark_seen(uid) return message.body[ 'plain' ][ 0 ] |
首先我們用with語句,打開郵箱。然后通過下面語句獲取所有的未讀郵件:
1
|
all_msg = box.messages(unread = True ) |
獲取未讀郵件后,對郵件進行遍歷。將主題為“Reomte Control”的郵件標記為已讀,并返回文本內容。
這里需要注意,因為我們篩選出了主題為“Remote Control”的郵件,因此我們在用手機發郵件的時候需要將主題設置為“Remote Control”,這樣可以避免其它郵件的干擾。
四、截圖
截圖需要使用到PIL模塊,安裝如下:
1
|
pip install pillow |
截圖的代碼很簡單:
1
2
3
4
5
6
7
8
9
|
from PIL import ImageGrab def grab(sender, to): # 截取電腦全屏 surface = ImageGrab.grab() # 將截屏保存為surface.jpg surface.save( 'surface.jpg' ) # 將截屏發送給手機 send_mail(sender, to, [ 'surface.jpg' ]) |
其中send_mail的代碼如下:
1
2
3
4
5
|
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) |
關于發送郵件的介紹可以參考上面提到的博客。
五、關機
關機的操作非常簡單,我們可以用python來執行命令行語句即可。代碼如下:
1
2
3
4
5
|
import os def shutdown(): # 關機 os.system( 'shutdown -s -t 0' ) |
除了關機,我們還可以執行很多操作。對于一些復雜的操作,我們可以預編寫一些bat文件,這里就不演示了。
六、完整代碼
上面我們編寫了各個部分的代碼,然后再來看看主體部分的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
def main(): # 電腦用來發送郵件已經電腦讀取的郵箱 password = '********' # 手機端的郵箱 # 讀取郵件的時間間隔 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) |
我們可以根據自己的需求編寫一些其它功能。下面是完整的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
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 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(): password = '你的授權碼' 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實現用手機監控遠程控制電腦的方法的文章就介紹到這了,更多相關Python 手機監控遠程控制電腦內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/ZackSock/article/details/116177092