Python은 휴대전화로 원격 제어 컴퓨터를 감시하는 방법을 실현한다

1. 앞말


많은 경우에 우리는 원격 제어 컴퓨터의 수요가 있다.예를 들어 어떤 물건을 다운로드하고 있으면 컴퓨터를 다운로드한 후에 꺼야 한다.아니면 프로그램의 운행 상황을 감시해야 합니다.
오늘 우리는 Python으로 컴퓨터를 원격 감시하고 제어하는 작은 프로그램을 실현할 것이다.

2. 실현 원리


듣자니 원격 제어 컴퓨터는 매우 고급스러운 것 같지만, 실현하기는 사실 매우 간단하다.실현 원리는 다음과 같다.
  • 프로그램을 실행하여 프로그램이 끊임없이 메일을 읽도록 합니다
  • 핸드폰으로 컴퓨터에 메일을 보냅니다
  • 지정한 주제의 메일을 읽었는지 판단하고 있으면 메일 내용을 가져옵니다
  • 메일 내용에 따라 미리 설정된 함수를 실행합니다
  • 원격 제어 컴퓨터를 배우는 것보다 메일을 읽는 법을 배우는 것이 낫다.물론 위의 절차는 원격 제어 컴퓨터만 실현했을 뿐 컴퓨터에 대한 감시는 실현되지 않았다.모니터링 조작은 캡처로 할 수 있다.
    우리는 메일 내용이grab인 것을 읽을 때 컴퓨터 캡처를 보내는 지령을 미리 설정할 수 있다.어떻게 컴퓨터 캡처를 휴대전화 메일박스에 보내면 모니터링 효과에 도달할 수 있다.
    메일을 보내는 방법에 관해서는 블로그를 참고할 수 있습니다파이썬으로 메일을 보내는 방법 ?.여기는 더 이상 상세하게 말하지 않겠습니다.다음은 우편물을 어떻게 읽는지 봅시다.

    3. 메일 읽기


    메일을 읽으려면 imbox 모듈을 사용해야 합니다. 설치 문장은 다음과 같습니다.
    
    pip install imbox
    
    메시지를 읽는 코드는 다음과 같습니다.
    
    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 문장으로 메일박스를 엽니다.그리고 다음 문장을 통해 읽지 않은 모든 메일을 가져옵니다.
    
    all_msg = box.messages(unread=True)
    읽지 않은 메일을 가져와서 메일을 훑어보십시오.제목이 "Reomte Control"인 메시지를 읽은 것으로 표시하고 텍스트 내용을 되돌려줍니다.
    "Remote Control"이라는 주제의 메일을 선택했기 때문에 휴대전화로 메일을 보낼 때 다른 메일의 방해를 피하기 위해 "Remote Control"이라는 주제를 설정해야 합니다.

    4. 캡처


    캡처는 PIL 모듈에 사용해야 합니다. 설치는 다음과 같습니다.
    
    pip install pillow
    
    캡처된 코드는 간단합니다.
    
    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 코드는 다음과 같습니다.
    
    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)
    
    메일 발송에 대한 소개는 위에서 언급한 블로그를 참고할 수 있다.

    5. 끄기


    전원을 끄는 작업은 매우 간단합니다. 우리는python으로 명령행 문장을 실행하면 됩니다.코드는 다음과 같습니다.
    
    import os
    
    def shutdown():
     #  
        os.system('shutdown -s -t 0')
    
    
    전원을 끄는 것 외에 우리는 많은 조작을 실행할 수 있다.일부 복잡한 조작에 대해 우리는bat 파일을 미리 작성할 수 있는데, 여기에서는 설명하지 않을 것이다.

    6. 전체 코드


    위에서 우리는 각 부분의 코드를 작성한 다음에 주체 부분의 코드를 다시 보았다.
    
    def main():
     #  
        username = '[email protected]'
        password = '********'
     
     #  
        receiver = '[email protected]'
     
     #  
        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)
    
    우리는 자신의 수요에 따라 다른 기능을 작성할 수 있다.다음은 전체 코드입니다.
    
    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():
        username = '[email protected]'
        password = ' '
        receiver = '[email protected]'
        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 핸드폰 원격 제어 컴퓨터에 관한 내용은 저희 이전의 글을 검색하거나 아래의 관련 글을 계속 훑어보시기 바랍니다. 앞으로 많은 응원 부탁드립니다!

    좋은 웹페이지 즐겨찾기