背景
有時(shí)本地服務(wù)器的時(shí)間不準(zhǔn)了,需要同步互聯(lián)網(wǎng)上的時(shí)間。
解決方案
* 由于NTP時(shí)間同步,如果相差比如有好幾個(gè)小時(shí),那么時(shí)間不同步矯正回來其實(shí)是非常慢的;我本次主要就是講第2種方案,通過Python來實(shí)現(xiàn)的,可以直接設(shè)置為互聯(lián)網(wǎng)上的時(shí)間。
要點(diǎn)描述
代碼實(shí)現(xiàn)
代碼見:https://github.com/smilejay/python/blob/master/py2018/set_check_localtime.py
代碼在CentOS 7.4系統(tǒng)上Python 2.7上正常運(yùn)行
為了考慮到兼容性和運(yùn)行的方便性,代碼中發(fā)送HTTP請(qǐng)求沒有使用最流行的requests庫而是使用了urllib2這個(gè)Python標(biāo)準(zhǔn)庫。
# -*- coding: utf-8 import sys import time import subprocess import argparse import urllib2 def set_beijing_time_from_web(url): ''' set os and hardware clock as beijing time from internet ''' # use urllib2 in python2; not use requests which need installation response = urllib2.urlopen(url) #print response.read() # 獲取http頭date部分 ts = response.headers['date'] # 將日期時(shí)間字符轉(zhuǎn)化為time gmt_time = time.strptime(ts[5:25], "%d %b %Y %H:%M:%S") # 將GMT時(shí)間轉(zhuǎn)換成北京時(shí)間 local_time = time.localtime(time.mktime(gmt_time) + 8*3600) str1 = "%u-%02u-%02u" % (local_time.tm_year, local_time.tm_mon, local_time.tm_mday) str2 = "%02u:%02u:%02u" % ( local_time.tm_hour, local_time.tm_min, local_time.tm_sec) cmd = 'date -s "%s %s"' % (str1, str2) #print cmd subprocess.check_call(cmd, shell=True) hw_cmd = 'hwclock -w' #print hw_cmd subprocess.check_call(hw_cmd, shell=True) print 'OK. set time: %s' % ' '.join([str1, str2]) def check_localtime_with_internet(url): ''' check local time with internet ''' threshold = 2 # use urllib2 in python2; not use requests which need installation response = urllib2.urlopen(url) #print response.read() # 獲取http頭date部分 ts = response.headers['date'] # 將日期時(shí)間字符轉(zhuǎn)化為time gmt_time = time.strptime(ts[5:25], "%d %b %Y %H:%M:%S") # 將GMT時(shí)間轉(zhuǎn)換成北京時(shí)間 internet_ts = time.mktime(gmt_time) local_ts = time.mktime(time.gmtime()) if abs(local_ts - internet_ts) <= threshold: print 'OK. check localtime.' else: print 'ERROR! local_ts: %s internet_ts:%s' % (local_ts, internet_ts) sys.exit(1) if __name__ == '__main__': url = 'http://www.baidu.com' parser = argparse.ArgumentParser() parser.description = 'set/check localtime (i.e. CST) with internet' parser.add_argument('-c', '--check', action='store_true', help='only check local time') parser.add_argument('-s', '--set', action='store_true', help='only set local time') parser.add_argument('-u', '--url', default=url, help='the url to sync time') args = parser.parse_args() if args.set: set_beijing_time_from_web(args.url) else: check_localtime_with_internet(args.url)
網(wǎng)站題目:Python檢查和同步本地時(shí)間(北京時(shí)間)的實(shí)現(xiàn)方法-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://chinadenli.net/article4/dgdgie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、微信小程序、網(wǎng)站營銷、建站公司、移動(dòng)網(wǎng)站建設(shè)、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容