Scenario: When a Web monitor matches a corresponding keyword, the matching content is put into the Redis database. The Redis database is constantly increasing over time.
Function: Realize when the data in Redis database increases, want the user to send an email, remind to view.
Code:
# -*- coding: utf-8 -*-' @note: to enable monitoring of the Redis database with changes to send mail ' Import redisimport timeimport smtplib from email.mime.text import mimetext red=redis. Strictredis ("172.16.2.156", 6379,8) mailto_list=[' [email protected] '] #邮件接收方mail_host = " Smtp.163.com " #设置邮件服务器mail_user =" ABCD " #用户名mail_pass =" abcd123 " #口令 mail_postfix= "163.com" #发件箱的后缀 def send_mail (to_list,sub,content): me= "Scan_result" + "<" +mail_user+ "@" +mail_postfix+ ">" msg = mimetext (content,_subtype= ' plain ', _charset= ' gb2312 ') msg[' Subject '] = sub msg[' from '] = me msg[' to '] = ";". Join (to_list) try: &nbsP; server = smtplib. SMTP () server.connect (mail_host) server.login (Mail_user,mail_pass) server.sendmail (Me, to_list, msg.as_string ()) server.close () return true except exception, e: print str (e) return false def loop (): lis=red.hgetall (' Alert '). VALUES () num=len (LIS) time.sleep lis=red.hgetall (' alert '). VALUES () num_2=len (LIS) if num==num_2: print ' No new keyword added ' else: if send_mail (mailto_list, "keywords", "there have a keyword added Please check inredis databases ... "): print "Send email success ..." else: print "Send email false ..." if __name__ = = ' __main__ ': while true: try: loop () excEpt: continue
This article is from the "Black Time" blog, so be sure to keep this source http://blacktime.blog.51cto.com/11722918/1795312
Python Send mail