zoukankan      html  css  js  c++  java
  • python数据库查询监控

    #!/usr/bin/python2.7
    # encoding: utf-8

    #import MySQLdb as MySQLdb
    import MySQLdb
    import traceback
    import time
    import smtplib
    from email.mime.text import MIMEText

    def getCurrentDateTime():
    now=int(time.time())
    timeArray=time.localtime(now)
    todayDate=time.strftime("%Y-%m-%d",timeArray)
    todayTime=time.strftime("%H:%M:%S",timeArray)
    todayDateTime=time.strftime("%Y-%m-%d %H:%M:%S",timeArray)
    return todayDateTime

    def sendMail(tomail,subject,body):
    HOST="aaa.123.com"
    SUBJECT=subject
    TO=tomail
    FROM="11@aa.com"
    text=body

    msg=MIMEText(text,"plain","utf-8")
    msg['Subject'] = SUBJECT
    msg['From'] = FROM
    msg['To'] = ','.join(TO)

    try:
    server=smtplib.SMTP()
    server.connect(HOST,"25")
    server.starttls()
    server.login("11@aa.com","bbbbbb")
    server.sendmail(FROM,TO,msg.as_string())
    server.quit()
    print "邮件发送成功"
    except Exception,e:
    print "失败"+str(e)

    def connRemoteMysql(*arg):

    ip = arg[0]
    user = arg[1]
    passwd = arg[2]
    port = arg[3]

    try:
    conn = MySQLdb.connect(host=ip,user=user,passwd=passwd,port=port,charset='utf8') #关键字参数
    #cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) #传递参数cursors.DictCursor返回字典结构
    cursor = conn.cursor() #获取操作游标
    conn.select_db('aaa') #得到当前指向数据库的指针
    #sql1 = 'select * from aaa_user ;'
    sql2 = 'select count(*) from bbb_user;'
    cursor.execute(sql2) #执行命令方法
    tuple1 = cursor.fetchall() #接收返回值方法
    tuple2 = tuple1[0]
    result = tuple2[0]
    #print "当前注册用户数为:%s" % r #内置%格式化字符串,
    #print "aaaa:{num}人。".format(num=result) #str.format()字符串处理函数
    #conn.commit()
    return result
    except:
    traceback.print_exc()
    conn.rollback()
    finally:
    cursor.close()
    conn.close()
    if __name__ == "__main__":

    ip = "192.168.11.1"
    user = "aaa"
    passwd = "bbbbb"
    port = 3306
    person = connRemoteMysql(ip,user,passwd,port)

    tomail = ['11@qq.com']
    body = "aaa:{}".format(person)
    subject = "aaa-%s" % getCurrentDateTime()
    sendMail(tomail, subject, body)

  • 相关阅读:
    C# WinForm程序中强制退出程序以及启动程序
    WinForm程序中对关闭按钮的操作
    DataTable转换为List
    json日期转换
    ajax局部刷新
    程序暂停或延迟几秒再次运行
    分页显示的制作流程
    Django 项目基础操作总结
    Django初识
    Mysql有关分页的操作
  • 原文地址:https://www.cnblogs.com/gaoyanbing/p/12581794.html
Copyright © 2011-2022 走看看