示例#1
0
import os
import smtplib

app=Flask(__name__)

app.config.from_pyfile('OSHomeConfig',silent=True)

ADMINS = ['****@126.com']
if not app.debug:
    import logging
    from logging.handlers import SMTPHandler

    mail_handler = SMTPHandler('smtp.163.com',
                               '****@163.com',
                               ADMINS, 'YourApplication Failed')
    mail_handler.username=""
    mail_handler.password=""
    mail_handler.setLevel(logging.ERROR)
    app.logger.addHandler(mail_handler)


def connect_db():
    return sqlite3.connect(app.config['DATABASE'])

def init_db():
    if os.path.exists(app.config['DATABASE']):
        pass
    else:
        with closing(connect_db()) as db:
            with app.open_resource('schema.sql', mode='r') as f:
                db.cursor().executescript(f.read())
def main():

    parser = argparse.ArgumentParser(
        'doorbot', description='camera monitoring and activity detection' )

    verbosity_grp = parser.add_mutually_exclusive_group()

    verbosity_grp.add_argument(
        '-v', '--verbose', action='store_true',
        help='show debug messages on stdout' )

    verbosity_grp.add_argument(
        '-m', '--metric', action='store_true',
        help='show fps counts every so often on stdout' )

    verbosity_grp.add_argument(
        '-q', '--quiet', action='store_true',
        help='be as silent as possible on stdout' )

    parser.add_argument(
        '-c', '--config', action='store', default=None,
        help='manually specify a location for the config file to use' )

    parser.add_argument(
        '-o', '--option', action='append', nargs=3,
        metavar=('section', 'option', 'value'),
        help='specify a manual override for the given option' )

    parser.add_argument(
        '-f', '--fps', action='store', default=10.0,
        help='master loop FPS' )

    args = parser.parse_args()

    if not args.option:
        args.option = []

    config = DoorbotConfig( args.config, args.option )

    if args.verbose:
        logging.basicConfig( level=logging.DEBUG )
        #logging.getLogger( 'doorbot.run' ).setLevel( logging.INFO )
        #logging.getLogger( 'doorbot.process' ).setLevel( logging.WARNING )
        #logging.getLogger( 'camera.process' ).setLevel( logging.WARNING )
        logging.getLogger( 'urllib3.connectionpool' ).setLevel( logging.WARNING )
        logging.getLogger( 'fps.timer' ).setLevel( logging.WARNING )
        logging.getLogger( 'framelock' ).setLevel( logging.ERROR )
    elif args.quiet:
        logging.basicConfig( level=logging.ERROR )
    else:
        logging.basicConfig( level=logging.INFO )
        logging.getLogger( 'framelock' ).setLevel( logging.ERROR )
        logging.getLogger( 'fps.timer' ).setLevel( logging.ERROR )
    logger = logging.getLogger( 'main' )

    if args.metric:
        logging.getLogger( 'fps.timer' ).setLevel( logging.DEBUG )

    try:
        smtp_server = urlparse( config.parser['exceptions']['smtpserver'] )
        smtp_handler = SMTPHandler(
            (smtp_server.hostname,
                smtp_server.port if smtp_server.port else 25),
            config.parser['exceptions']['smtpfrom'],
            config.parser['exceptions']['smtpto'].split( ',' ),
            '[doorbot] Exception Occurred' )
        if smtp_server.username:
            smtp_handler.username = smtp_server.username
        if smtp_server.password:
            smtp_handler.password = smtp_server.password
        if 'smtps' == smtp_server.scheme:
            smtp_handler.secure = ()
        smtp_handler.setLevel( logging.ERROR )
        logging.getLogger( '' ).addHandler( smtp_handler )
        logger.info( 'exception reporter initialized' )

    except (NoOptionError, NoSectionError, KeyError) as exc:
        logger.info( 'could not setup exception reporter: %s', exc )

    #app.start()
    #app.join()
    app = None
    try:
        app = Doorbot( config, fps=args.fps )

        app.run()
    except KeyboardInterrupt:
        logger.info( 'quitting on ctrl-c' )
        if app:
            for camera_key in app.cameras:
                app.cameras[camera_key].stop()
            app.overlay_thread.stop()
            for observer_key in app.observer_procs:
                app.observer_procs[observer_key].stop()
        sys.exit( 0 )

    except Exception as exc: # pylint: disable=broad-except
        logger.exception( exc )
示例#3
0
logger.setLevel(logging.WARN)

# 创建日志处理器(StreamHandler, FileHandler)
handler1 = logging.StreamHandler()
handler1.setLevel(logging.INFO)

handler2 = logging.FileHandler('django.log')
handler2.setLevel(logging.ERROR)

handler3 = SMTPHandler('smtp.qq.com',
                       fromaddr='*****@*****.**',
                       toaddrs='*****@*****.**',
                       subject='日志邮箱通知')

# 配置邮件验证用户和口令(或授权码)
handler3.username = '******'
handler3.password = '******'  # QQ邮箱发件时验证的“授权码”
handler3.setLevel(logging.FATAL)

# 创建上传日志请求的处理器HTTPHandler
# 上传日志的接口: http://10.35.162.97:5000/log/
# 接口的方法: post
# 响应的数据是json
handler4 = HTTPHandler(host='10.35.162.97:5000',
                       url='/log/', method='POST')
handler4.setLevel(logging.INFO)


# 创建格式化对象 , 并添加到处理器
formatter = logging.Formatter('[ <%(asctime)s> %(name)s %(levelname)s ] %(message)s')
handler1.setFormatter(formatter)