1
我想在启动时运行一个python脚本,这将需要一个约10秒的视频应用外部输入(如按钮,红外传感器等,在我们的情况下超声波传感器),然后使用Python的SMTPlib库将此视频邮寄到指定的电子邮件地址。smtplib发送多条消息,而不是一个
所有这一切都工作正常。但是,多次使用此输入时,树莓派会将多个视频(通过过去按下按钮拍摄)发送到电子邮件地址,而不是由最后一个输入启动的视频。因此,按1次按钮将发送1个视频;推它2次会发送一个新的一个和最后一个;推3次将发送一个新的和最后两个,以此类推。
我甚至尝试在python脚本中发送邮件后立即插入一个os.remove()。运行程序后,运行ls显示文件确实被删除。然而,不知何故,这些被删除的视频进入电子邮件。当使用smtplib时,它会被存储在内存中的某个位置吗?
中提及脚本如下:
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
emailfrom = "[email protected]"
emailto = "[email protected]"
username = "[email protected]"
password = "pass"
msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = emailto
msg["Subject"] = "Email Subject -- "
msg.preamble = "Email Body --"
while(True):
d=0
# Possibly the relevant section
d = pulse_d*17150
d= round(d, 2)
if(d<100):
with picamera.PiCamera() as camera:
camera.start_preview()
camera.start_recording('/home/pi/video.h264')
time.sleep(5)
camera.stop_recording()
camera.stop_preview()
time.sleep(5)
fileToSend = "/home/pi/video.h264"
ctype, encoding = mimetypes.guess_type(fileToSend)
if ctype is None or encoding is not None:
GPIO.output(test, True)
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
fp = open(fileToSend, "rb")
GPIO.output(test,False)
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
fp.close()
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)
msg.attach(attachment)
server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login(username,password)
server.sendmail(emailfrom, emailto, msg.as_string())
server.quit()
os.remove("/home/pi/video.h264")
工作。感谢:D –