[英]Raspberrypi Python code not working proprely
我想使用此代码从我的 raspberry_pi 获取 cpu 温度,如果温度过高发送警告电子邮件,代码如下所示:
# coding=utf-8
import os
import smtplib
from email.mime.text import MIMEText
# At First we have to get the current CPU-Temperature with this defined function
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return (res.replace("temp=", "").replace("'C\n", ""))
# Now we convert our value into a float number
temp = float(getCPUtemperature())
# Check if the temperature is abouve 30°C (test with 30)
if (temp > 30):
# Enter your smtp Server-Connection
server = smtplib.SMTP('smtpmail.provider.com', 587)
# if your using gmail: smtp.gmail.com
server.ehlo()
server.starttls()
server.ehlo
# Login
server.login("your email or username", "your Password")
# Now comes the Text we want to send:
value = "Critical warning! The actual temperature is: " + getCPUtemperature()
msg = MIMEText(value)
# The Subject of your E-Mail
msg['Subject'] = "Critical warning! Temperature:" + getCPUtemperature()
# Consigner of your E-Mail
msg['From'] = "Raspberry Pi"
# recipient of your E-Mail
msg['To'] = "recipient@gmail.com"
# Finally send the mail
server.sendmail("consigner@gmail.com", "recipient@gmail.com", msg.as_string())
server.quit()
print "Everything was working fine! Best regards www.quaintproject.wordpress.com"
这段代码应该可以工作并且有以前的成功示例,但是当我运行它时,输出如下:
pi@raspberrypi:~ $ sudo nano tempwarn.py
pi@raspberrypi:~ $ sudo python tempwarn.py
Traceback (most recent call last):
File "tempwarn.py", line 20, in <module>
server = smtplib.SMTP('smtpmail.provider.com', 587)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 317, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 292, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
为什么会出现这种错误? 我很确定 587 是 gmail 的正确设置。
将 smtpmail.provider.com 交换为 smtp.gmail.com
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.