功能并非原创, 只是重复实现了 http://codecloud.net/python-control-128.html 中描述的功能.
实现功能: 通过给固定邮件地址发送命令(包含在主题中)的方式控制远程电脑实现相应功能(譬如关机等, 可以根据实际需要实现更复杂的功能)
实现原理: 远程电脑不断(每隔10s)利用 python 脚本检查邮件服务器是否有新邮件, 如有则对邮件进行分析, 如果包含可执行命令, 则执行相应命令
你需要的: 装有 PYTHON 的 PC; 最好两个邮箱(一个用于接收命令, 一个用来发邮件); 可选手机邮件客户端(可以实现在手机上发送邮件, 更高大上)
注意: 不要使用163邮箱来接受命令, 每隔 10s 的检查间隔会导致网易拒绝让你登录的~
我反正丧心病狂用学校的服务器. Python 代码如下:
1
#
-*- coding: utf-8 -*-
2
3
import
poplib,email,re
4
from email.header import decode_header
5import smtplib
6from email.mime.text import MIMEText
7import time
8import os,sys
9import random
101112def send_mail(to_mail_addr, message):
13 from_mail_addr = "****@fudan.edu.cn"14 password = "****"15 smtp_server = "mail.fudan.edu.cn"#smtp_server1617 message = "Hello, mission received: n t t" + message
18 msg = MIMEText(message, ‘plain‘, ‘gb2312‘)
19 msg[‘Subject‘] = ‘This is a reply msg by PythonMailControl‘20 msg[‘From‘] = from_mail_addr
21 msg[‘To‘] = to_mail_addr
2223 server = smtplib.SMTP(smtp_server, 25)
24 server.set_debuglevel(1)
25 server.login(from_mail_addr, password)
26 server.sendmail(from_mail_addr, [to_mail_addr], msg.as_string())
27 server.quit()
282930def get_last_mail():
31try:
32 host = ‘mail.fudan.edu.cn‘# pop3 server33 username = ‘****@fudan.edu.cn‘34 password = ‘****‘35 p = poplib.POP3_SSL(host)
36 p.user(username)
37 p.pass_(password)
38 ret = p.stat()
39except poplib.error_proto,e:
40print"Login failed:",e
41return (-2,0,0) # error code, nonsense, nonsense4243 mail_number = ret[0] # return the total number of mails44 ret = p.list()
45 down = p.retr(1) # return the last received mail46return (1,mail_number,down)
474849if__name__ == "__main__":
50 mail_number_old = 0
51while 1:
52 (error,mail_number,down) = get_last_mail()
53if (error==1) and (mail_number != mail_number_old): # no error and have new mail54 mail_number_old = mail_number
55for line in down[1]: # check the mail line by line, get the sender and subject56if line[0:4]=="From":
57 from_mail_addr = re.search( ‘[0-9a-zA-Z_]*@[0-9a-zA-Z_.]*‘, line).group(0)
58if line[0:7]=="Subject":
59 content = line[9:]
60if content[0] == ‘@‘: # response according to the subject61 cmd = content[1:]
62try:
63 os.system(cmd) # do sth.64 send_mail(from_mail_addr,cmd) # reply message to the mail sender65except:
66 os.system(‘echo error‘)
67 time.sleep(10)
运行待机情况:
然后用手机给我的学号邮箱发个邮件. 主题是 @shutdown -s -t 600
其中 @ 是前导字符, 用于给程序判断这是一个命令. shutdown 等则是10分钟后关机的命令.
等待5-10s, 在PC上就出现了等待关机的提示
同时, 手机上也受到了回复的邮件.
完.
然而暂时并没有什么卵用~
原文:http://www.cnblogs.com/willowfly/p/4769553.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!