1 Star 0 Fork 111

王怀军/MySQLMTOP

forked from 茹憶/MySQLMTOP 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
global_functions.py 2.63 KB
一键复制 编辑 原始数据 按行查看 历史
ruzuojun 提交于 2017-07-28 15:36 . 代码目录结构重新调整和优化
#!/bin/env python
#-*-coding:utf-8-*-
import MySQLdb
import string
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import ConfigParser
import smtplib
from email.mime.text import MIMEText
from email.message import Message
from email.header import Header
def get_config(group,config_name):
config = ConfigParser.ConfigParser()
config.readfp(open('./etc/config.ini','rw'))
config_value=config.get(group,config_name).strip(' ').strip('\'').strip('\"')
return config_value
host = get_config('monitor_server','host')
port = get_config('monitor_server','port')
user = get_config('monitor_server','user')
passwd = get_config('monitor_server','passwd')
dbname = get_config('monitor_server','dbname')
def mysql_exec(sql,param):
conn=MySQLdb.connect(host=host,user=user,passwd=passwd,port=int(port),connect_timeout=5,charset='utf8')
conn.select_db(dbname)
cursor = conn.cursor()
if param <> '':
cursor.execute(sql,param)
else:
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
def mysql_query(sql):
conn=MySQLdb.connect(host=host,user=user,passwd=passwd,port=int(port),connect_timeout=5,charset='utf8')
conn.select_db(dbname)
cursor = conn.cursor()
count=cursor.execute(sql)
if count == 0 :
result=0
else:
result=cursor.fetchall()
return result
cursor.close()
conn.close()
def get_option(key):
conn=MySQLdb.connect(host=host,user=user,passwd=passwd,port=int(port),connect_timeout=5,charset='utf8')
conn.select_db(dbname)
cursor = conn.cursor()
sql="select value from options where name=+'"+key+"'"
count=cursor.execute(sql)
if count == 0 :
result=0
else:
result=cursor.fetchone()
return result[0]
cursor.close()
conn.close()
mail_host = get_config('mail_server','mail_host')
mail_user = get_config('mail_server','mail_user')
mail_pass = get_config('mail_server','mail_pass')
mail_postfix = get_config('mail_server','mail_postfix')
def send_mail(to_list,sub,content):
'''
to_list:发给谁
sub:主题
content:内容
send_mail("aaa@126.com","sub","content")
'''
me=mail_user
#me=mail_user+"<</span>"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content, _subtype='html', _charset='utf8')
msg['Subject'] = Header(sub,'utf8')
msg['From'] = Header(me,'utf8')
msg['To'] = ";".join(to_list)
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me,to_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/whj888/MySQLMTOP.git
git@gitee.com:whj888/MySQLMTOP.git
whj888
MySQLMTOP
MySQLMTOP
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385