python实现树莓派开机自动发送IP到指定邮箱

 1 #!/usr/bin/python
 2 # -*- coding:UTF-8 -*-
 3 #测试发送邮件163邮箱发送到qq邮箱
 4 import smtplib
 5 from email.mime.text import MIMEText
 6 from email.header import Header
 7 from email.utils import formataddr
 8 
 9 msg=MIMEText('请输入内容','plain','utf-8')
10 msg['From']='发送邮箱昵称 <xxx@163.com>'
11 msg['To']='接收邮箱昵称 <xxx@qq.com>'
12 #主题(主题不能为test等会被拦截)
13 msg['Subject']=Header('niceday','utf-8')
14 
15 server=smtplib.SMTP()
16 server.connect('smtp.163.com',25)
17 #密码为第三方客户端密码不是登录密码
18 server.login('xxx@163.com','密码')
19 server.sendmail('xxx@163.com','xxx@qq.com',msg.as_string())
20 server.quit()
 1 #-*- coding: utf-8 -*-
 2 #获取树莓派所有网络设备IP
 3 import array
 4 import struct
 5 import socket
 6 import fcntl
 7 
 8 SIOCGIFCONF = 0x8912
 9 SIOCGIFADDR = 0x8915
10 BYTES = 4096         
11 sck = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
12 names = array.array('B',b'\0' * BYTES)
13 bytelen = struct.unpack('iL', fcntl.ioctl(sck.fileno(), SIOCGIFCONF, struct.pack('iL', BYTES, names.buffer_info()[0])))[0]
14 namestr = names.tostring()
15 ifaces = [namestr[i:i+32].split('\0', 1)[0] for i in range(0, bytelen, 32)]
16     
17 #再获取每个接口的IP地址
18 iplist = []
19 for ifname in ifaces:
20     ip = socket.inet_ntoa(fcntl.ioctl(sck.fileno(),0x8915,struct.pack('256s',ifname[:15]))[20:24])
21         iplist.append(ifname+':'+ip)
22 
23 ip = '\r\n'.join(iplist)
24 print iplist
 1 #检测网络
 2 import urllib
 3 def check_network():
 4     while True:
 5         try:
 6             result=urllib.urlopen('http://baidu.com').read()
 7             print result
 8             print "Network is Ready!"
 9             break
10         except Exception , e:
11             print e
12             print "Netwofk is not ready,Sleep 5s..."
13             time.sleep(5)
14         return True
15 
16 check_network()

 

posted @ 2017-06-14 19:44  SStriver  阅读(1869)  评论(0编辑  收藏  举报