#!/usr/bin/python
# -*- coding: UTF-8 -*-
import datetime
import smtplib
import string
import MySQLdb
Yesterday=datetime.datetime.now() - datetime.timedelta(days=1)
yesterday=Yesterday.strftime('%Y-%m-%d')
logfile="/app/newgetui/wyethService/current/logs/DataApiInfo_%s-0.log" %yesterday
sumline=0
type1=0
type2=0
type3=0
type4=0
type5=0
type6=0
type7=0
type8=0
with file(logfile,'r') as lock_f:
for line in lock_f:
if line.find("Result info") > -1:
sumline=sumline+1
else:
print "no file"
if line.find("checkResult=1") > -1:
type1=type1+1
elif line.find("checkResult=2") > -1:
type2=type2+1
elif line.find("checkResult=3") > -1:
type3=type3+1
elif line.find("checkResult=4") > -1:
type4=type4+1
elif line.find("checkResult=5") > -1:
type5=type5+1
elif line.find("checkResult=6") > -1:
type6=type6+1
elif line.find("checkResult=7") > -1:
type7=type7+1
elif line.find("checkResult=8") > -1:
type8=type8+1
print type8
def intomysql():
try:
conn=MySQLdb.connect(host='xxxxx',user='xxxx',passwd='xxxx',port=3306,db='xxx')
cur=conn.cursor()
yesterdaystr='"%s"' %yesterday
cur.execute('insert into wyeth_offline_interface_count values(null,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)' %(yesterdaystr,type1,type2,type3,type4,type5,type6,type7,type8,sumline))
conn.commit()
cur.close()
conn.close()
except MySQLdb.Error,e:
print 'insert faild:',e
intomysql()
HOST = "xxxx.20.102" #定义smtp主机
SUBJECT = "wyeth线下接口查询统计" #定义邮件主题
TO = ["zhoull@getui.com"] #定义邮件收件人
FROM = "xxxxxx" #定义邮件发件人
text = " %s 惠氏线下接口主机122查询统计如下 \n checkResult1 num %s \n checkResult2 num %s \n checkResult3 num %s \n checkResult4 num %s \n checkResult5 num %s \n checkResult6 num %s \n checkResult7 num %s \n checkResult8 num %s \n 合计 num %s \n" %(yesterday,type1,type2,type3,type4,type5,type6,type7,type8,sumline) #邮件内容
BODY = string.join(( #组装sendmail方法的邮件主体内容,各段以"\r\n"进行分隔
"From: %s" % FROM,
"To: %s" % TO,
"Subject: %s" % SUBJECT ,
"",
text
), "\r\n")
server = smtplib.SMTP() #创建一个SMTP()对象
server.connect(HOST,"25") #通过connect方法连接smtp主机
server.starttls() #启动安全传输模式
server.login("xxxxx","xxxxx") #邮箱账号登录校验
for i in range(0,1):
server.sendmail(FROM, TO[i], BODY) #邮件发送
server.quit()
python 插入数据
最新推荐文章于 2022-05-01 17:55:55 发布