我需要弄清楚如何創建一個腳本來掃描目錄中的新文件,當有新文件時,通過電子郵件發送文件。通過python發送視頻文件
有人在我的公寓樓裏偷自行車!首先,這是我的錯(我要鎖定它),現在是通過切割鏈升級的騙子。在騙子偷走我的第二輛自行車時,我用1/2英寸的飛機電線割下了它。
無論如何,使用樹莓派作爲運動激活的安全攝像頭,我希望它在視頻節目完成錄製後立即向我發送視頻文件。這是他們偷了pi。
我正在看這些例子,但我不知道如何使腳本連續運行(每分鐘)或如何使它掃描一個新文件的文件夾。
How do I send attachments using SMTP?
OK 我得到它下降到掃描,然後試圖電子郵件。嘗試附加視頻文件時失敗。你能幫我嗎?下面是修改後的代碼:
失敗是:
味精= MimeMultipart的() 類型錯誤: 'LazyImporter' 對象不是可調用38行
import time, glob
import smtplib
import email.MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders, MIMEMultipart
import os
#Settings:
fromemail= "Jose Garcia <[email protected]>"
loginname="[email protected]"
loginpassword="somerandomepassword"
toemail= "Jose Garcia <[email protected]>"
SMTPserver='smtp.gmail.com'
SMTPort=587
fileslocation="/Users/someone/Desktop/Test/*.mp4"
subject="Security Notification"
def mainloop():
files=glob.glob(fileslocation) #Put whatever path and file format you're using in there.
while 1:
new_files=glob.glob(fileslocation)
if len(new_files)>len(files):
for x in new_files:
if x in files:
print("New file detected "+x)
print("about to call send email")
sendMail(loginname, loginpassword, toemail, fromemail, subject, gethtmlcode(), x, SMTPserver, SMTPort)
files=new_files
time.sleep(1)
def sendMail(login, password, to, frome, subject, text, filee, server, port):
# assert type(to)==list
# assert type(filee)==list
msg = MIMEMultipart()
msg['From'] = frome
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(text))
# #for filee in files:
part = MIMEBase('application', "octet-stream")
part.set_payload(open(filee,"rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(filee))
msg.attach(part)
smtp = smtplib.SMTP(SMTPserver, SMTPort)
smtp.sendmail(frome, to, msg.as_string())
server.set_debuglevel(1)
server.starttls()
server.ehlo()
server.login(login, password)
server.sendmail(frome, to, msg)
server.quit()
def gethtmlcode():
print("about to assemble html")
html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" '
html +='"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">'
html +='<body style="font-size:12px;font-family:Verdana"><p>A new video file has been recorded </p>'
html += "</body></html>"
return(html)
#Execute loop
mainloop()
更好的,但到底是什麼「它失敗「是什麼意思?它發送[Rick Astley]的視頻(http://en.wikipedia.org/wiki/Rickrolling)?它會引發錯誤?什麼是錯誤信息? – 2014-09-04 17:00:57
我再次修改它,減少混亂。代碼仍然失敗,「失敗是: msg = MIMEMultipart()TypeError:'LazyImporter'對象不可調用,第38行」 – 2014-09-04 17:44:54
請告訴我們什麼'LazyImporter'和「行38」是。 – 2014-09-04 20:53:36