天天看点

python通过外部参数发送邮件

#!/usr/bin/python

#coding:utf8

#file topid.py

import smtplib,mimetypes     #mail module

import time            #system time   

import sys,os 

from email.mime.text import MIMEText  #mail type mode

from email.mime.multipart import MIMEMultipart

from email.mime.image import MIMEImage

#sys and date messages

datetime = time.strftime('%Y-%m-%d %H:%M:%S')  #get time 

dataname = "tlcytop"

#mail messages

msg = MIMEMultipart()

msg['From'] = "[email protected]

msg['To'] = "[email protected]

msg['Subject'] = "dataname %s time %s" %(dataname,datetime) 

smtp = smtplib.SMTP()

smtp.connect("smtp.audiocn.com")

smtp.login("[email protected]",'password')

a=sys.argv[1] #传递参数

a=int(a)   #数据类型转换

if (a < 450):

#ddprint "ok %s" % (a) 

txt = MIMEText("Subject: %s tlcytop.edit_ugc-------->updatetime<450   updatetime= %s" % (datetime,a ))

msg.attach(txt)

smtp.sendmail('[email protected]', '[email protected]',msg.as_string())

smtp.sendmail('[email protected]', '[email protected]',msg.as_string()) #发送邮件

smtp.quit()

调用python的shell脚本

#/bin/bash

#

#a=`/usr/bin/mysql -uroot  -p123456 -e "select count(user) from mysql.user;" |sed -n '2p'`

a=`/usr/bin/mysql -uxxx -hxxx -pxxxx -e "select COUNT(*) from tlcytop.edit_ugc where updatetime > '2016-01-07 00:00:00'" |sed -n '2p'`

source ~/.bashrc && /usr/bin/python /home/tiantao/mysqldcheck.py $a

本文转自 吃草的青蛙 51CTO博客,原文链接:http://blog.51cto.com/tlinux/1733061,如需转载请自行联系原作者