#-*-coding:utf-8 -*-
#!/usr/bin/python
import sys
reload(sys) # reload 才能调用 setdefaultencoding 方法
sys.setdefaultencoding(‘utf-8‘) # 设置 ‘utf-8‘
import MySQLdb
import houtai_dbname
from email.mime.text import MIMEText
from email.header import Header
import smtplib
#第三方服务smtp
mail_host=‘smtp.yeah.net‘
mail_user=‘xxxx‘
mail_pass=‘xxxx‘
mail_postfix= ‘yeah.net‘
receivers=‘[email protected]‘
def get_addserver():
number=" "
global conn,plname
defdb=houtai_dbname.check_plname(plname)
try:
cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
conn.select_db(defdb) #选择数据库
sql= "SELECT count(*) as num,state from serverinfo where pf=\‘%s\‘ and zoneid<8000 GROUP BY state order by state" % (plname)
cur.execute(sql) #执行sql
result=cur.fetchall() #获取的数据集结
cur.close() #关闭游标
conn.close() #关闭数据库
except MySQLdb.Error,e:
#print "MySQLdb error %d:%s " % (e.args[0],e.args[1])
print e
print result
for i in result:
number=plname+","+number+str(i[‘num‘])+","
#stri="当前该平台已开区 %s" % (result[‘num‘])
print number
return number
def send_mail(mail_to,subject,content):
me = mail_user+"<"+mail_user+"@"+mail_postfix+">" #邮件发送方
message = MIMEText(content, ‘plain‘, ‘utf-8‘) #邮件内容
message[‘subject‘] = subject #邮件标题
message[‘from‘] = me #发送方
message[‘to‘] = receivers #接收方
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host) #连接smtp
smtpObj.login(mail_user,mail_pass) #登录
smtpObj.sendmail(me,receivers, message.as_string()) #获取参数发送邮件
smtpObj.close() #关闭
print "邮件发送成功"
except Exception,e:
print "ERROR,无法发送邮件"
print e
if __name__=="__main__":
id=" "
dic=houtai_dbname.houtaidic
for i in dic:
#print i,dic[i]
plname= i
conn=MySQLdb.connect(host="%s" % dic[i],user=‘xxxx‘,passwd=‘xxx‘,port=3306,charset=‘utf8‘)
max_server=get_addserver()
id=id+max_server
#print id
send_mail(‘[email protected]‘,‘platfrom open servervices‘,id)
原文地址:http://blog.51cto.com/yzg784534398/2063785