有没有更好的方法在python中编写此脚本

时间:2016-12-13 17:42:04

标签: python email scripting alerts

我知道这是懒散的,我是初学者,但有更好的方法来做到这一点,我觉得有,但我不知道。我要做的是,每当分数变化高于9时发送电子邮件提醒。

以下是代码:

#!/usr/bin/env python

"""
Script to send any pending alerts via email
"""

import sys
from pymongo import MongoClient
from datetime import datetime

#### begin


def send_email(): 
print "Begin GAMER-ALERT-SEND-EMAILS %s " % (datetime.now()).

if 'score_by_cat' >=9: 
        send_email()

 if 'score_by_cat' >=9:
       print( "Send Email")
    else:
       print('Nothing")




print "Begin GAMER-ALERT-SEND-EMAILS %s " % (datetime.now())


client = MongoClient('mongodb://localhost:28057/')
db = client.factor

email_rcpt_list = ['example@momo.com']
                  ['example2@momo.com']  

#process the alert..  if it is high enough risk and the makes sense (not a security control etc)

    for alert in db.risk_alerts.find({"$and" : [{'sent': False}]}):
        if float(alert['metric'])>=9 and alert['category']!='security control':
            #Call email send here
            for email in email_rcpt_list:
                print 'Call email and pass in the email address from email_rcpt_list'
                print ' Sample alert:  Vendor: %s  Has an new item %s on %s  of elevated risk %s in the category %s link: https://Gamer-dev.momo.net/profile/view/detail/%s' % (alert['vendor_name'],alert['key'],alert['source'],alert['metric'],alert['category'],alert['profile_id']) 
    #mark as sent always
    db.risk_alerts.update_one({"_id":alert['_id']},{"$set": {"sent":True}})

print "End GAMER-ALERT-SEND-EMAILS %s" % (datetime.now())

1 个答案:

答案 0 :(得分:0)

查看代码的前几行:

if 'score_by_cat' >=9

print "Begin GAMER-ALERT-SEND-EMAILS %s " % (datetime.now())
timestamp=datetime.utcnow()

条件语句(例如ifelsefor)必须以冒号(:)结尾。我认为第二行应缩进(如果分数> = 9,则必须发送电子邮件)。此外,此代码中未使用timestamp=datetime.utcnow();因此,你可以删除这一行。

现在请重新检查您的代码是否存在潜在的格式错误。