Use Python to send and receive mail for remote PC control

Source: Internet
Author: User
Tags python script

The function is not original, but the function described in http://codecloud.net/python-control-128.html is implemented repeatedly.

Implementation: Control the remote PC by sending commands (included in the topic) to a fixed email address to achieve the appropriate functions (such as shutdown, etc., can achieve more complex functions according to actual needs)

The principle of implementation: The remote computer constantly (every 10s) using a Python script to check the mail server for new messages, if any, to analyze the message, if it contains executable commands, execute the appropriate command

What you need: a PYTHON-loaded PC; Preferably two mailboxes (one for receiving commands, one for sending mail); Can be player mail client (can be implemented on the phone to send mail, on the larger)

Note: Do not use 163 mailbox to accept the command, every 10s check interval will cause NetEase refused to let you log in ~

I'm desperate to use the school server anyway. The Python code is as follows:

1 #-*-coding:utf-8-*-2 3 ImportPoplib,email,re4  fromEmail.headerImportDecode_header5 ImportSmtplib6  fromEmail.mime.textImportMimetext7 Import Time8 ImportOs,sys9 ImportRandomTen  One  A defsend_mail (TO_MAIL_ADDR, message): -FROM_MAIL_ADDR ="* * * * @fudan. edu.cn" -Password ="****" theSmtp_server ="mail.fudan.edu.cn"  #Smtp_server -  -Message ="Hello, Mission received: \ n \ t \ t"+message -msg = mimetext (Message,'Plain','gb2312') +msg['Subject'] ='This was a reply msg by Pythonmailcontrol' -msg[' from'] =from_mail_addr +msg[' to'] =to_mail_addr A  atServer = Smtplib. SMTP (Smtp_server, 25)  -Server.set_debuglevel (1) - server.login (from_mail_addr, password) - Server.sendmail (FROM_MAIL_ADDR, [to_mail_addr], msg.as_string ()) - server.quit () -  in  - defget_last_mail (): to     Try: +Host ='mail.fudan.edu.cn'  #POP3 server -Username ='* * * * @fudan. edu.cn' thePassword ='****' *p =Poplib. Pop3_ssl (host) $ P.user (username)Panax Notoginseng p.pass_ (password) -RET =P.stat () the     exceptpoplib.error_proto,e: +         Print "Login failed:", E A         return( -2,0,0)#error code, nonsense, nonsense the  +Mail_number = ret[0]#return The total number of mails -RET =p.list () $Down = P.RETR (1)#return the last received mail $     return(1, Mail_number,down) -  -  the if __name__=="__main__":  -Mail_number_old =0Wuyi      while1: the(error,mail_number,down) =Get_last_mail () -         if(error==1) and(Mail_number! = Mail_number_old):#no error and have new mail WuMail_number_old =Mail_number -              forLineinchDOWN[1]:#Check the mail line by line, get the sender and subject About                 ifline[0:4]==" from": $FROM_MAIL_ADDR = Re.search ('[0-9a-za-z_]*@[0-9a-za-z_.] *', line). Group (0) -                 ifline[0:7]=="Subject": -Content = Line[9:] -             ifContent[0] = ='@':#response according to the subject Acmd = content[1:] +                 Try: theOs.system (CMD)#Do sth. -Send_mail (From_mail_addr,cmd)#reply message to the Mail sender $                 except: theOs.system ('echo Error') theTime.sleep (10)

To run the standby situation:

Then use the mobile phone to my study number mailbox to send an e-mail. Subject is @shutdown-S-T 600

Where @ is the leading character and is used to tell the program that this is a command. Shutdown is the command to shut down after 10 minutes.

Waiting for 5-10s, on the PC on the waiting for the shutdown prompt

At the same time, the phone also received a reply to the mail.

Finish.

But for the time being there's no egg to use ~

Use Python to send and receive mail for remote PC control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.