本文实例为大家分享了Python实现远程操控电脑的具体代码,供大家参考,具体内容如下
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
53
54
55
56
57
58
59
60
61
62
|
import poplib
import sys
import smtplib
from email.mime.text import MIMEText
import os
from email.header import decode_header
import email
import time
def check_email():
try :
p = poplib.POP3( 'pop.163.com' )
p.user( '*****@163.com' )
p.pass_( '*******' )
ret = p.stat()
except :
print ( 'Login failed!' )
sys.exit( 1 )
str = p.top(ret[ 0 ], 0 )
strlist = []
for x in str [ 1 ]:
try :
strlist.append(x.decode())
except :
try :
strlist.append(x.decode( 'gbk' ))
except :
strlist.append((x.decode( 'big5' )))
mm = email.message_from_string( '\n' .join(strlist))
sub = decode_header(mm[ 'subject' ])
if sub[ 0 ][ 1 ]:
submsg = sub[ 0 ][ 0 ].decode(sub[ 0 ][ 1 ])
else :
submsg = sub[ 0 ][ 0 ]
if submsg.strip() = = '关机' :
return 0
elif submsg.strip() = = '重启' :
return 1
p.quit()
def send_email():
user = '*******@qq.com'
pwd = '**********'
to = [ '*****@163.com' , '*****@139.com' ] #139邮件会有短信提醒,让我知道是否成功
msg = MIMEText('')
msg[ 'Subject' ] = '已收到命令!'
msg[ 'From' ] = user
msg[ 'To' ] = ',' .join(to)
s = smtplib.SMTP( 'smtp.qq.com' )
s.login(user, pwd)
s.sendmail(user, to, msg.as_string())
s.close()
if __name__ = = '__main__' :
while True :
time.sleep( 20 )
if check_email() = = 0 :
send_email()
os.system( 'shutdown -s -t 1' ) #关机
break
if check_email() = = 1 :
send_email()
os.system( 'shutdown -r' ) #重启
break
|
测试成功
其它命令自行添加,这里只作 关机和重启. 脑洞大开!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/u013511642/article/details/44259557