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 –