通过python发送视频文件

通过python发送视频文件,python,security,email,raspberry-pi,Python,Security,Email,Raspberry Pi,我需要弄清楚如何制作一个脚本来扫描目录中的新文件,当有新文件时,通过电子邮件发送文件 有人一直在我的公寓里偷自行车!首先是我的错(因为我把它锁上了),现在这个骗子通过切割链条升级了。我是在那个骗子割断了1/2英寸的飞机电线偷走了我的第二辆自行车后得到的 无论如何,使用树莓pi作为运动激活安全摄像头,我希望它在视频程序完成录制后立即向我发送一个视频文件。这是以防他们偷了pi 我正在看这些示例,但我不知道如何使脚本连续(每分钟)运行,或者如何使它扫描文件夹以查找新文件 正常 我把它归结为扫描,然后

我需要弄清楚如何制作一个脚本来扫描目录中的新文件,当有新文件时,通过电子邮件发送文件

有人一直在我的公寓里偷自行车!首先是我的错(因为我把它锁上了),现在这个骗子通过切割链条升级了。我是在那个骗子割断了1/2英寸的飞机电线偷走了我的第二辆自行车后得到的

无论如何,使用树莓pi作为运动激活安全摄像头,我希望它在视频程序完成录制后立即向我发送一个视频文件。这是以防他们偷了pi

我正在看这些示例,但我不知道如何使脚本连续(每分钟)运行,或者如何使它扫描文件夹以查找新文件

正常
我把它归结为扫描,然后尝试发送电子邮件。尝试附加视频文件时失败。你能帮忙吗?修订后的守则如下:

故障是:
msg=MIMEMultipart() TypeError:“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 <somerandomemail@gmail.com>"
loginname="somerandomemail@gmail.com"
loginpassword="somerandomepassword"
toemail= "Jose Garcia <somerandomemail@gmail.com>"
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()
导入时间,全局
导入smtplib
导入email.MIMEMultipart
从email.MIMEBase导入MIMEBase
从email.MIMEText导入MIMEText
从email.Utils导入COMMASPACE,formatdate
来自电子邮件导入编码器,MIMEMultipart
导入操作系统
#设置:
fromemail=“何塞·加西亚”
登录名=”somerandomemail@gmail.com"
loginpassword=“somerandomepassword”
toemail=“何塞·加西亚”
SMTPserver='smtp.gmail.com'
SMTPort=587
fileslocation=“/Users/someone/Desktop/Test/*.mp4”
主题=“安全通知”
def mainloop():
files=glob.glob(filelocation)#将您正在使用的任何路径和文件格式放在其中。
而1:
新建文件=glob.glob(文件位置)
如果len(新文件)>len(文件):
对于新文件中的x:
如果文件中有x:
打印(“检测到新文件”+x)
打印(“即将呼叫发送电子邮件”)
sendMail(loginname、loginpassword、toemail、fromemail、subject、gethtmlcode()、x、SMTPserver、SMTPort)
文件=新文件
时间。睡眠(1)
def sendMail(登录名、密码、收件人、发件人、主题、文本、文件、服务器、端口):
#断言类型(to)==列表
#断言类型(文件)=列表
msg=MIMEMultipart()
msg['From']=frome
msg['To']=COMMASPACE.join(To)
msg['Date']=formattate(localtime=True)
msg['Subject']=主语
msg.attach(MIMEText(text))
##对于文件中的文件:
part=MIMEBase(“应用程序”、“八位字节流”)
part.set_有效载荷(open(filee,“rb”).read())
编码器。编码_base64(部分)
part.add_头('Content-Disposition','attachment;filename=“%s”'
%os.path.basename(文件名))
附加信息(部分)
smtp=smtplib.smtp(SMTPserver,SMTPort)
smtp.sendmail(frome、to、msg.as_string())
服务器。设置调试级别(1)
server.starttls()
server.ehlo()
server.login(登录名,密码)
sendmail(frome、to、msg)
server.quit()
def gethtmlcode():
打印(“即将汇编html”)
html=“”
html+='已录制新的视频文件

' html+=“” 返回(html) #执行循环 mainloop()
只需将脚本放入循环中,并让它休眠60秒。您可以使用
glob
获取目录中的文件列表<查看列表中的内容(即目录中的文件列表)时,中的code>非常有用


我终于让它工作了。我不得不使用Python2.5来消除懒散的导入器错误。每次将新文件添加到安全文件夹时,它都会通过电子邮件发送给我。伐木被破坏了

import time, glob
import smtplib
from email.MIMEMultipart import MIMEMultipart 
from email.MIMEMultipart import MIMEBase 
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
import datetime 
from email import Encoders
import os
import sys


#Settings:
fromemail= 'RaspberryPI Security Camera <someone>'
loginname='someone@gmail.com'
loginpassword='something'
toemail= 'Jose Garcia < someone@gmail.com>'
SMTPserver='smtp.gmail.com'
SMTPort=587
fileslocation='/Users/someone/Desktop/Test/*.*'
subject="Security Notification"
log='logfile.txt'

def mainloop():   
        files=glob.glob(fileslocation) #Put whatever path and file format you're using in there.
        while 1:
            f = open(log, 'w')
            sys.stdout = Tee(sys.stdout, f)
            new_files=glob.glob(fileslocation)
            if len(new_files)>len(files):
                for x in new_files:
                    if x in files:
                        print(str(datetime.datetime.now()) + "New file detected "+x)
                        sendMail(loginname, loginpassword, toemail, fromemail, subject, gettext(), x, SMTPserver, SMTPort)
            files=new_files
            f.close()
            time.sleep(1)

def sendMail(login, password, send_to, send_from, subject, text, send_file, server, port):

    msg = MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = COMMASPACE.join(send_to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach( MIMEText(text) )

    part = MIMEBase('application', "octet-stream")
    part.set_payload( open(send_file,"rb").read() )
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(send_file))
    msg.attach(part)

    smtp = smtplib.SMTP(SMTPserver, SMTPort)
    smtp.set_debuglevel(1) 
    smtp.ehlo() 
    smtp.starttls() 
    smtp.login(login, password) 
    smtp.sendmail(send_from, send_to, msg.as_string() )
    smtp.close()


def gettext():
    text = "A new file has been added to the security footage folder. \nTime Stamp: "+ str(datetime.datetime.now())
    return(text)

class Tee(object):
    def __init__(self, *files):
        self.files = files
    def write(self, obj):
        for f in self.files:
            f.write(obj)


#Execute loop
mainloop()
导入时间,全局
导入smtplib
从email.MIMEMultipart导入MIMEMultipart
从email.MIMEMultipart导入MIMEBase
从email.MIMEText导入MIMEText
从email.Utils导入COMMASPACE,formatdate
导入日期时间
从电子邮件导入编码器
导入操作系统
导入系统
#设置:
fromemail='RaspberryPI安全摄像头'
罗金纳姆酒店someone@gmail.com'
loginpassword='something'
toemail='Jose Garcia'
SMTPserver='smtp.gmail.com'
SMTPort=587
fileslocation='/Users/someone/Desktop/Test/*.'
主题=“安全通知”
log='logfile.txt'
def mainloop():
files=glob.glob(filelocation)#将您正在使用的任何路径和文件格式放在其中。
而1:
f=打开(日志“w”)
sys.stdout=三通(sys.stdout,f)
新建文件=glob.glob(文件位置)
如果len(新文件)>len(文件):
对于新文件中的x:
如果文件中有x:
打印(str(datetime.datetime.now())+“检测到新文件”+x)
sendMail(loginname、loginpassword、toemail、fromemail、subject、gettext()、x、SMTPserver、SMTPort)
文件=新文件
f、 关闭()
时间。睡眠(1)
def sendMail(登录名、密码、发送至、发送自、主题、文本、发送文件、服务器、端口):
msg=MIMEMultipart()
msg['From']=send_From
msg['To']=COMMASPACE.join(发送到)
msg['Date']=formattate(localtime=True)
msg['Subject']=主语
msg.attach(MIMEText(text))
part=MIMEBase(“应用程序”、“八位字节流”)
part.set_有效负载(打开(发送文件“rb”).read())
编码器。编码_base64(部分)
part.add_头('Content-Disposition','attachment;filename=“%s”'%os.path.basename(发送_文件))
附加信息(部分)
smtp=smtplib.smtp(SMTPserver,SMTPort)
smtp.set_调试级别(1)
smtp.ehlo()
smtp.starttls()
smtp.login(登录名,密码)
smtp.sendmail(发送发件人、发送收件人、msg.as\u字符串())
smtp.close()
def gettext():
text=“已将新文件添加到安全镜头文件夹。\n时间戳:”+str(datetime.datetime.now())
返回(文本)
类(对象):
定义初始化(self,*文件):
self.files=文件
def写入(自、obj):
对于self.files中的f:
f、 写入(obj)
#执行循环
mainloop()
T
import time, glob
import smtplib
from email.MIMEMultipart import MIMEMultipart 
from email.MIMEMultipart import MIMEBase 
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
import datetime 
from email import Encoders
import os
import sys


#Settings:
fromemail= 'RaspberryPI Security Camera <someone>'
loginname='someone@gmail.com'
loginpassword='something'
toemail= 'Jose Garcia < someone@gmail.com>'
SMTPserver='smtp.gmail.com'
SMTPort=587
fileslocation='/Users/someone/Desktop/Test/*.*'
subject="Security Notification"
log='logfile.txt'

def mainloop():   
        files=glob.glob(fileslocation) #Put whatever path and file format you're using in there.
        while 1:
            f = open(log, 'w')
            sys.stdout = Tee(sys.stdout, f)
            new_files=glob.glob(fileslocation)
            if len(new_files)>len(files):
                for x in new_files:
                    if x in files:
                        print(str(datetime.datetime.now()) + "New file detected "+x)
                        sendMail(loginname, loginpassword, toemail, fromemail, subject, gettext(), x, SMTPserver, SMTPort)
            files=new_files
            f.close()
            time.sleep(1)

def sendMail(login, password, send_to, send_from, subject, text, send_file, server, port):

    msg = MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = COMMASPACE.join(send_to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach( MIMEText(text) )

    part = MIMEBase('application', "octet-stream")
    part.set_payload( open(send_file,"rb").read() )
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(send_file))
    msg.attach(part)

    smtp = smtplib.SMTP(SMTPserver, SMTPort)
    smtp.set_debuglevel(1) 
    smtp.ehlo() 
    smtp.starttls() 
    smtp.login(login, password) 
    smtp.sendmail(send_from, send_to, msg.as_string() )
    smtp.close()


def gettext():
    text = "A new file has been added to the security footage folder. \nTime Stamp: "+ str(datetime.datetime.now())
    return(text)

class Tee(object):
    def __init__(self, *files):
        self.files = files
    def write(self, obj):
        for f in self.files:
            f.write(obj)


#Execute loop
mainloop()
from email.mime.text import MIMEText