Python system time vs. MySQL time

Source: Internet
Author: User

because they are responsible for overseas projects, often encounter some problems, recently by the system time and MySQL time is not a time zone, and pits themselves, generally modified the system time zone, MySQL must restart, or the MySQL time zone is not right, will cause the data are all wrong ~ ~ ~, hey, Only the pit to oneself, will think to avoid this kind of thing again, so use Python to write a simple time zone of the script, time zone is not and mail sent out, everyone reference, details as follows:

1. Script instance

#!/usr/bin/env python# coding=utf8# auther:kuangl# this is system time  and sql time difffrom  datetime  import  *import os, sys,socket,fcntl,structimport mysqldbimport smtplibfrom email.mime.multipart import  mimemultipartfrom email.mime.base import mimebasefrom email.mime.text import  Mimetextreload (SYS) sys.setdefaultencoding (' UTF8 ') ' defines the Send mail function ' ' Def sendmail (html,emailaddress,mailsubject , from_address= "[email protected]"):         mail_list= Emailaddress.split (",")         msg=mimemultipart ()          msg[' accept-language ']= ' ZH-CN '          msg[' accept-charset ']=  ' iso-8859-1,utf-8 '         msg[' from ']=from _address         msg[' to ']= ";". Join (mail_list)         msg[' Subject ']=mailsubject.decode ("Utf-8")         txt=mimetext (HTML, ' HTML ', ' Utf-8 ')          txt.set_charset (' Utf-8 ')         msg.attach (txt )         smtp=smtplib. SMTP ("mail.test.com")         smtp.sendmail (msg["from"],mail_list, Msg.as_string ())         smtp.close () "View native hostname" Hostname= Socket.gethostname () print hostname ' view native IP address ' def get_ip_address (ifname):     s =socket.socket (Socket.af_inet,socket. SOCK_DGRAM)     return socket.inet_ntoa (Fcntl.ioctl (             s.fileno (),              0x8915,            struct.pack (' 256s ', ifname[:15] ) (    ) [20:24]) ip_add = get_ip_address (' eth0 ') print ip_add ' ' View system time ' Nowtime = datetime.now () daytime = nowtime.strftime ('%y-%m-%d %h:%m ') print  ' System time time: ',  daytime ' view database time ' ' pwd= ' 111133334444 ' sql= ' Select now () ' Def mysql _connect (sql,host):     try:        conn= MySQLdb.connect (host= ' 127.0.0.1 ', user= ' Rd ', passwd=pwd,port=3306)          cur=conn.cursor ()         cur.execute (SQL)          result=cur.fetchall ()         cur.close ()         conn.close ()          return result    except mysqldb.error,e:        print  "Mysql Error  %d: %s " %  (E.args[0],e.args[1]) server_result=mysql_connect (sql=sql,host= ' 127.0.0.1 ') sql_gettime =server_result[0][0]sqltime=sql_gettime.strftime ('%y-%m-%d %h:%m ') print  ' SQL server time: ' , Sqltime '   Define message parameters ' ' content=  ' dear all: <br> &nbsp;&nbsp;  '   + ip_add +  '  , System and Database time error, <br>  &nbsp;&nbsp; System time is :  ' + daytime + '  <br>  &nbsp;&nbsp; Database time is :  '  + SQLTime +  '  <br> &nbsp;&nbsp; Please check all time! <br>  &nbsp;&nbsp; <br> &nbsp;&nbsp; friendly reminder,please note! ' subject=  ' [monitoring] [overseas]Zone monitoring ['  + hostname +  ']system and database time error '   Determine whether time is equal "' if daytime == sqltime:    print " system and sql time  is ok, not sednemail "else:    print " System and sql  time is fail,start email to all "    sendmail (Html=Content, Emailaddress= ' [email protected] ', mailsubject=subject)

2. Test results

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/58/DB/wKiom1S-Z5zRt5mTAADj3H9YT60609.jpg "title=" a.jpg " alt= "Wkiom1s-z5zrt5mtaadj3h9yt60609.jpg"/>

This article is from the "& Think Far Dawn" blog, make sure to keep this source http://kling.blog.51cto.com/3320545/1606401

Python system time vs. MySQL time

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.