- 论坛徽章:
- 0
|
回复 3楼 xichen 的帖子
import smtplib,time,MySQLdb
def sendmail(baseline,To):
mailserver = '192.168.0.10'
From = 'ha@haha.com'
to = [To,'haha@haha.com']
date = time.ctime(time.time())
text = ('From: %s\nTo: %s\nDate: %s +0800\nSubject: 提交制品\nContent-Type: text/plain;charset="gb2312"\nContent-Transfer-Encoding: 8bit\n\n明天提交 %s,\n如不能准时提交,请在sharepoint上填写软件问题报告单和变更申请单.'%(From,To,date,baseline))
try:
server = smtplib.SMTP(mailserver)
send = server.sendmail(From,to,text)
server.quit()
except:
error(baseline,1)
def error(log,id):
datetime = time.ctime(time.time())
file = open('error_log.txt','a')
if id == 1:
file.write('sendmail: ''%s was not sent!!! %s\n'%(log,datetime))
elif id == 2:
file.write('mysql: %s %s\n'%(log,datetime))
file.close()
def sql():
try:
connect = MySQLdb.connect(host = '127.0.0.1',port = 3306,user = 'root',passwd = 'lead',db = 'scm')
try:
find = 'select baseline,address from mail where time <= adddate(now(),interval 1 minute) and time >= subdate(now(),interval 1 minute)'
while 1:
cursor = connect.cursor()
cursor.execute(find)
for row in cursor.fetchall():
print row[0]
print row[1]
sendmail(row[0],row[1])
cursor.close()
time.sleep(120)
except:
connect.close()
error('command ',2)
except:
error('mysql was not connected',2)
sql()
谢谢版主,哈哈。以上是整个程序。sql其实是主函数,只调用一次,程序不修改数据只查询。我的意思是程序运行的同时,我自己连mysql并修改数据,在这种情况下我这个程序查询出来的不是最新的数据,还是修改之前的数据。搞不明白为什么。 |
|