#/bin/env python
# -*-coding:utf8-*-
import socket
import fcntl
import time
import struct
import smtplib
import urllib
import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
#发送邮件的基本函数,参数依次如下
# smtp服务器地址、邮箱用户名,邮箱秘密,发件人地址,收件人地址(列表的方式),邮件主题,邮件html内容
def sendEmail(smtpserver, username, password, sender, receiver, subject, msghtml):
msgRoot = MIMEMultipart('related')
msgRoot["To"] = ','.join(receiver)
msgRoot["From"] = sender
msgRoot['Subject'] = subject
msgText = MIMEText(msghtml,'html','utf-8')
msgRoot.attach(msgText)
#sendEmail
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
# 检查网络连同性
def check_network():
# 试验5次ping 百度,如果连通就返回True,否则返回False
for i in range(0, 5):
try:
result=urllib.urlopen('http://baidu.com').read()
#print result
print "Network is Ready!"
break
except Exception , e:
print e
print "Network is not ready,Sleep 5s...."
time.sleep(5)
else:
print "Sorry that pi isn't connectted to Internet now"
return False
return True
# 获得本级制定接口的ip地址
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("1.1.1.1",80))
ipaddr=s.getsockname()[0]
s.close()
return ipaddr
if __name__ == '__main__':
if check_network():
ipaddr = get_ip_address()
now = datetime.datetime.now()
time_info = now.strftime('%Y-%m-%d %A %H:%M:%S')
send_text = "Boot time: %s\nIP addr: %s" % (time_info, ipaddr)
sendEmail('smtp.sina.com','you email username ','your email password ','your emial address',['[email protected]'], 'Raspberry Pi boot status', send_text)
else:
print "Sorry that I can't help without network"
div>
Read More:
- Raspberry pie import opencv error: ImportError: numpy.core.multiarray failed to import
- Raspberry pie Use PCA9685() Error: [Errno 121] Remote I/O error
- Raspberry pie compilation and installation opencv error: fatal error: bootdesc_bgm.i: No such file or directory
- Python: How to Obtaining Publick IP Quickly
- When sending HTTP request, python encountered: error 54, ‘connection reset by peer’ solution
- When sending an email, an error was reported: AttributeError:’list’ object has no attribute’encode’
- Invalid python sd, Fatal Python error: init_fs_encoding: failed to get the Python cod [How to Solve]
- How to Solve Python WARNING: Ignoring invalid distribution -ip (e:\python\python_dowmload\lib\site-packages)
- [Solved] NPM install Error: check python checking for Python executable python2 in the PATH
- Python netmiko library Cisco telnet switch automation
- RuntimeError: Address already in use [How to Solve]
- [Pytorch Error Solution] Pytorch distributed RuntimeError: Address already in use
- Python time tuples are converted to timestamps, strings
- [Solved] DDP/DistributedDataParallel Error: RuntimeError: Address already in use
- Python: How to parses HTML, extracts data, and generates word documents
- Extracting Data from XML (Using Python to Access Web Data)
- [Solved] opencv-python: recipe for target ‘modules/python3/CMakeFiles/opencv_python3.dir/all‘ failed
- Python classes that connect to the database
- Python: How to Create an Automatic Recording Program