python windows时间同步工具
由于某種原因(BIOS電池沒電),電腦的系統時間會與我們的北京時間不同步,將會導致如下問題:
1. 搶火車票的時候已經過時間了
2.別的同事都走了,你還以為沒下班
……
?
規避問題的方法:同步系統時間
一. 獲取時間
在這里,我們有兩種方法
1. ?通過系統請求網站服務器頭部返回Respones Headers
獲取Date 參數值,修改系統時間
def getTime(self,url):conn = http.client.HTTPConnection(url)conn.request("GET", "/")r = conn.getresponse()# r.getheaders() #獲取所有的http頭ts = r.getheader('date') # 獲取http頭date部分# 將GMT時間轉換成北京時間ltime = time.strptime(ts[5:25], "%d %b %Y %H:%M:%S") # 格式tsttime = time.localtime(time.mktime(ltime) + 8 * 60 * 60) # +東八區dat = "date %u-%02u-%02u" % (ttime.tm_year, ttime.tm_mon, ttime.tm_mday)tm = "time %02u:%02u:%02u" % (ttime.tm_hour, ttime.tm_min, ttime.tm_sec)return [dat, tm]?
在這里解釋下strptime函數中,時間字符串的格式化說明
| %y 兩位數的年份表示(00-99) %a 本地簡化星期名稱 |
代碼中的%參數解析
%[flags][width][.precision][size]type
| %02u : |
?
2. 通過獲取授時中心的標準時間修改系統時間
授時中心的網址:
http://www.pool.ntp.org/
ntplib 下載地址:
https://pypi.python.org/pypi/ntplib/
?
二. 同步時間
獲取時間后,我們要同步時間,同步時間也有兩種方法
1.通過os.system 同步
def setTime(self,time):os.system(time[0])os.system(time[1])2.通過win32api.SetSystimeTime 同步
def setTime(self,time_cls):ttime = time.localtime(time.mktime(time_cls) - 8 * 60 * 60) # 本為東八區,卻set后多出8小時 UTC多了8個小時 , 所以……time_cls = ttimewin32api.SetSystemTime(time_cls.tm_year, time_cls.tm_mon, time_cls.tm_wday, time_cls.tm_mday, time_cls.tm_hour, time_cls.tm_min, time_cls.tm_sec, 0)?
完整代碼如下:
# -*- coding:utf-8 -*- # 1.獲取網絡時間 # 2.修改系統時間 import http.client import time import os,sys import ntplib import win32api''' '同步時間類 ''' class syctimes():def __init__(self):if sys.argv.__len__() == 1:try:time_list = self.getTime1()self.setTime1(time_list)except:time_list = self.getTime2('www.baidu.com')self.setTime2(time_list)for arg in sys.argv:if arg == '-1':time_list = self.getTime1()self.setTime1(time_list)if arg == '-2':time_list = self.getTime2('www.baidu.com')self.setTime2(time_list)sys.exit()def getTime1(self):c = ntplib.NTPClient()response = c.request('pool.ntp.org') # 授時中心的網址ts_stamp = response.tx_timets = time.localtime(ts_stamp)ttime = time.localtime(time.mktime(ts) + 8 * 60 * 60) # +東八區return tsdef getTime2(self,url):conn = http.client.HTTPConnection(url)conn.request("GET", "/")r = conn.getresponse()# r.getheaders() #獲取所有的http頭ts = r.getheader('date') # 獲取http頭date部分# 將GMT時間轉換成北京時間ltime = time.strptime(ts[5:25], "%d %b %Y %H:%M:%S") # 格式tsttime = time.localtime(time.mktime(ltime) + 8 * 60 * 60) # +東八區dat = "date %u-%02u-%02u" % (ttime.tm_year, ttime.tm_mon, ttime.tm_mday)tm = "time %02u:%02u:%02u" % (ttime.tm_hour, ttime.tm_min, ttime.tm_sec)return [dat, tm]'''修改時間1'''def setTime1(self,time_cls):ttime = time.localtime(time.mktime(time_cls) - 8 * 60 * 60) # 本為東八區,卻set后多出8小時 UTC多了8個小時 , 所以……time_cls = ttimewin32api.SetSystemTime(time_cls.tm_year, time_cls.tm_mon, time_cls.tm_wday, time_cls.tm_mday, time_cls.tm_hour, time_cls.tm_min, time_cls.tm_sec, 0)'''修改時間2'''def setTime2(self,time):os.system(time[0])os.system(time[1])if __name__ == "__main__":classSyc = syctimes()# 方法1:#time_list = classSyc.getTime1()#classSyc.setTime1(time_list)# 方法2:# time_list = classSyc.getTime2('www.baidu.com')# classSyc.setTime2(time_list)?
打包代碼為exe 添加隨系統啟動執行
import sysif __name__ == '__main__':from PyInstaller import __main__params = ['-F', '-w', '--icon=favicon.ico', 'times.py']__main__.run(params) # -*- coding:utf-8 -*- import win32api import win32conname = 'SycTimeTool' # 要添加的項值名稱 path = 'E:\dcb3688\時間同步工具.exe -1' # 要添加的exe路徑 # 注冊表項名 KeyName = 'Software\\Microsoft\\Windows\\CurrentVersion\\Run' # 異常處理 try:key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, KeyName, 0, win32con.KEY_ALL_ACCESS)win32api.RegSetValueEx(key, name, 0, win32con.REG_SZ, path)win32api.RegCloseKey(key) except:print('error') print('添加成功!')?
總結
以上是生活随笔為你收集整理的python windows时间同步工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 优秀简历要遵循哪些规则
- 下一篇: nodejs返回下载文档,文档名称出现汉