Python remote control of computer restart or shutdown through network mail, python mail

Source: Internet
Author: User

Python remote control of computer restart or shutdown through network mail, python mail

Requirement Analysis:

Python implements a project that remotely controls the restart or shutdown of a computer through network mail.

Procedure:

Process Analysis:

To implement this function, you must first control the restart and shutdown of the computer through Python locally, and then know how to remotely send messages to the Python program. You can execute the CMD command in Python to control the restart and shutdown of the computer, and send emails as remote control channels. The Python program automatically logs on to the email address detection email. When a shutdown command is sent to this email address, the command is executed.

1. Run the CMD command through the OS. system () function.

import os os.system('shutdown /s /t 1')

2. SMTP is a set of rules used to send mails from the source address to the destination address. It can control the transfer mode of mails. SMTP is a TCP/IP protocol cluster. The server specified by SMTP can send e-mail to the recipient's server.

import smtplib s=smtplib.SMTP('smtp.163.com') msg=s.ehlo() code=msg[0] print(msg) print("SMTP code is%d" %(code))

When sending E-mail, the customer should start the SMTP session with the EHLO command. If the command is successful, code 250 is returned.

3. POP is used to remotely manage emails on the server using the client. The most popular one is POP3, which belongs to the TCP/IP protocol cluster and is used to receive E-mail.

from poplib import POP3 s=poplib.POP3(pop3.163.com) s.user(username) s.pass_(password) ret=s.stat()

Specific implementation:

Control Computer Program:

Import email import smtplib import time import OS, sys import random def send_mail (): try: s = smtplib. SMTP ('smtp .163.com ') # connect to the server s. login ('fromusername', 'Password') # log on To send account msg = "To: tousername@163.com \ r \ nFrom: fromusername@163.com \ r \ nSubject: shutdown \ r \ n \ r \ nshutdown \ r \ n "# Set mail content s. sendmail ('fromusername @ 163.com ', 'tousername @ 163.com', msg) # send mail s. close () return 1 rows T (smtplib. SMTPException) as e: print e return 0 if _ name __= = '_ main _': while send_mail () = 0: # if sending fails, 10 seconds later. sleep (10)

Controlled computer program:

#-*-Encoding: UTF-8-*-import OS, sys, string import poplib import OS import time # pop3 server address host = "pop3.163.com" # username = "*********** @ 163.com" # password = "* * ***** "# obtain the complete information of the first email, in the return value, rows are stored in the down [1] list, and down [0] is the returned status information while True: # create a POP3 object, at this time, it is actually connected to the server p = poplib. POP3 (host) # Send the user name p. user (username) # Send password p to the server. pass _ (password) # list Mail Information. num indicates the number of mails, total_size indicates the total size of mails. num, total_size = p. stat () down = p. retr (num) # Get the latest mail to the tuples down # The 2nd elements of the tuples down are the mail content mails = down [1] print mails for line in mails: # traverse each line of the mail content if line. find ('subobject') = 0: # find the header line print line if line. find ('shutdown ')> 0: # If the title is shutdown p. dele (num) # Delete the email to prevent repeated shutdown next time. quit () # exit the mailbox print 'shutdown... 'OS. system ('shutdown/s/t 10') # Shut Down p. quit () # exit the mailbox time at the end of each round-robin. sleep (5) # sleep for every round of inquiry for 5 s

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.