人生第一个python脚本:从数据库查询数据并发送邮件
2018-01-22 13:39
267 查看
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time
date = time.strftime('%Y-%m-%d')
# 打开数据库连接
conn = MySQLdb.connect(
host='localhost',
port = 3306,
user='root',
passwd='',
db ='test',
charset="utf8",
)
# 使用cursor()方法获取操作游标
cur = conn.cursor()
# 使用execute方法执行SQL语句
data = cur.execute("select id,phone,num,name from test.user_info;")
# print data
info = cur.fetchmany(data)
cur.close()
conn.commit()
conn.close()
def dd(info):
s = ""
for i in info:
#print i[0]
#print i[1]
#print i[2]
#print i[3].decode('utf-8')
# 汉字需要 decode 成utf-8
s += '%s;%s;%s;%s#' % ( i[0],i[1],i[2],i[3].decode('utf-8') )
#print s
return s
# print(dd(info))
# print(date)
# 发送邮件
msg_from='123456@QQ.com' #发送方邮箱
passwd='XXXXXX' #填入发送方邮箱的授权码
msg_to='789@qq.com' #收件人邮箱
subject="python邮件测试 + date "#主题
content= str(dd(info)) #正文
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = msg_to
try:
s = smtplib.SMTP_SSL("smtp.exmail.qq.com",465)#邮件服务器及端口号
s.login(msg_from, passwd)
s.sendmail(msg_from, msg_to, msg.as_string())
print "发送成功"
except s.SMTPException,e:
print "发送失败"
finally:
s.quit()
=====================================
系统安装MySQL-python-1.2.5下载 :https://pypi.python.org/pypi/MySQL-python
# yum install –y mysql-devel
安装:
[root@www soft]# unzip MySQL-python-1.2.5.zip[root@www soft]# cd MySQL-python-1.2.5[root@www soft]# python setup.py build[root@www soft]# python setup.py install测试:[root@www soft]# python>>> import MySQLdb>>>安装完毕。
# -*- coding: UTF-8 -*-
import MySQLdb
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time
date = time.strftime('%Y-%m-%d')
# 打开数据库连接
conn = MySQLdb.connect(
host='localhost',
port = 3306,
user='root',
passwd='',
db ='test',
charset="utf8",
)
# 使用cursor()方法获取操作游标
cur = conn.cursor()
# 使用execute方法执行SQL语句
data = cur.execute("select id,phone,num,name from test.user_info;")
# print data
info = cur.fetchmany(data)
cur.close()
conn.commit()
conn.close()
def dd(info):
s = ""
for i in info:
#print i[0]
#print i[1]
#print i[2]
#print i[3].decode('utf-8')
# 汉字需要 decode 成utf-8
s += '%s;%s;%s;%s#' % ( i[0],i[1],i[2],i[3].decode('utf-8') )
#print s
return s
# print(dd(info))
# print(date)
# 发送邮件
msg_from='123456@QQ.com' #发送方邮箱
passwd='XXXXXX' #填入发送方邮箱的授权码
msg_to='789@qq.com' #收件人邮箱
subject="python邮件测试 + date "#主题
content= str(dd(info)) #正文
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = msg_to
try:
s = smtplib.SMTP_SSL("smtp.exmail.qq.com",465)#邮件服务器及端口号
s.login(msg_from, passwd)
s.sendmail(msg_from, msg_to, msg.as_string())
print "发送成功"
except s.SMTPException,e:
print "发送失败"
finally:
s.quit()
=====================================
系统安装MySQL-python-1.2.5下载 :https://pypi.python.org/pypi/MySQL-python
# yum install –y mysql-devel
安装:
[root@www soft]# unzip MySQL-python-1.2.5.zip[root@www soft]# cd MySQL-python-1.2.5[root@www soft]# python setup.py build[root@www soft]# python setup.py install测试:[root@www soft]# python>>> import MySQLdb>>>安装完毕。
相关文章推荐
- SQL Server 定时发送邮件(从数据库查询出的数据作为附件)
- linux shell实现从数据库中查询数据并把数据用csv格式发送邮件
- python脚本获取zabbix监控数据,并且通过邮件发送
- python查询数据库并发送邮件
- python连接oracel数据库,提取数据后制图并通过邮件发送
- 定时发邮件(ASP.net中每隔一定时间查询数据库表,并根据表里面的条件发送邮件)
- docker(7):使用python 连接数据库,插入并查询数据--link
- Python自动发送邮件的脚本源码
- 系统访问日志统计及邮件发送脚本--python实现
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- Shell查询数据库,和发送邮件
- Shell查询数据库,和发送邮件
- python脚本发送报警邮件
- zabbix 发送邮件脚本(python shell)
- sql脚本查询数据库表,数据,结构,约束等操作的方法
- Zabbix 自定义邮件发送脚本(Python) 推荐
- 数据库实用脚本1---查询数据库中有数据的表名和表中数据的行数
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- Python写的smtp发送邮件脚本
- 面面观 | 使用python 连接数据库,插入并查询数据--link