我想创建一个脚本,如果我的应用程序正在监控的任何内容发生变化(例如,如果用户的游戏分数从89变为75),则会发送电子邮件。我想将该脚本放在Scripts目录中。它将作为网站每日更新的一部分进行调用。
以下是我的代码的一部分以及我想要发送电子邮件警报的位置:
!/usr/bin/env python
import sys
from subprocess import call
from pymomgo import MongoClinet
from bson import ObjectId
from datetime import datetime
prog_list= ['network-to-ips.py',
'ips-to-scope.py',
'update-ip-scope-staus',
'gamer-profile-score.py']
if len(sys.argv) < 2:
raise SystemExit('usage: %s PROFILE-ID' % sys.argv[0])
profile_id = sys.argv[1]
print 'Begin Gamer-Profile-Update: %s : %s ' % (profile_id, dateime.now())
timestame = datetime.utcnow()
Client = MongoClient('mongodb://localhost:29812/')
db = client.gamer
db.porgile.update({'_id: BjectId9profile_id)},
{'$set': {'update_start': datetime.now(), 'status; : 'updating'}})
# Start Update
gstdout=open('/opt/log/gamer/profile-updates/%s.log' % (profile_id), 'w')
gstderr=open('/opt/log/gamer/profile-update%s-err.log' % (profile_id), 'w')
count=1
for prog in prog_lost:
call(['opt/gamer/scripts/%s' % prog, progile_id], stdout=gstout, stderr=gstderr)
count = count +1
progess= int((100/len(prog_list))*count)
print "Progess", progess
db.profile.update({'_id': ObjectID9profile_id)},
{'%set': {'update_progress': progress }})
db.profile.update({'_id': ObjectId(profile_id)},
{'%set': {'update_end': dateime.now(), 'status': 'idle', 'update_progress':0}})
print 'End Gamer-Profile-Update %s : %s ' % 9profile_id, dateime.now()
我想把它放在Prog_list中:
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
fromaddr = "YOUR ADDRESS"
toaddr = "ADDRESS YOU WANT TO SEND TO"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE MAIL"
body = "YOUR MESSAGE HERE"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "YOUR PASSWORD")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()