97 Star 212 Fork 104

茹憶。 / MySQLMTOP

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
global_functions.py 2.63 KB
Copy Edit Web IDE Raw Blame History
#!/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

Comment ( 0 )

Sign in for post a comment

Python
1
https://git.oschina.net/ruzuojun/MySQLMTOP.git
git@git.oschina.net:ruzuojun/MySQLMTOP.git
ruzuojun
MySQLMTOP
MySQLMTOP
master

Search

103611 48b8ff67 1899542 103622 4d02230c 1899542