網路校時(非NTP)

2021-09-26 02:31:49 字數 1508 閱讀 4851

windows一用就是幾年,總會碰到個更改系統時間的時候。可是把系統時間調壞了容易,要調出個正確可就沒那麼簡單了。幾年前(還上大學的時候,大概是大三吧)用c#寫過乙個ntp的程式,但是時過境遷,ntp的ip更改實在太頻繁了(難道是因為沒有商業利潤?);windows的「internet 時間」我從來就沒有試成功過(有人知道why嘛?);國家授時中心(

的什麼時間精靈自從windows變sp2後在我的電腦上也不能用了----這算什麼時代?。

看來,無奈下只好找個http伺服器來進行網路校時了?反正像我們這種凡人也不需要那種不差毫釐的時間,再說就憑windows那種時間精度,唉,啥也不說了.........

import

socket,urllib2,win32api,time

defupdatesystime():

try:

srv=

urllib2.urlopen(

'')    

date

=srv.headers.dict[

'date']

gmt=

time.strptime(date[5:

25], 

"%d %b %y %h:%m:%s")

##      setsystemtime函式的引數是使用gmt時間

return

win32api.setsystemtime(gmt.tm_year,gmt.tm_mon,gmt.tm_wday,gmt.tm_mday,gmt.tm_hour,gmt.tm_min,gmt.tm_sec,0)

except

:return

0def

printcurtime():

now 

=time.localtime(time.time())

print

time.strftime(

"%z %y-%m-%d %h:%m:%s

", now)

##超時時間,以 秒 為單位

timeout

=0.3

socket.setdefaulttimeout(timeout)

printu'

'printcurtime()

print

'\r\n

'i,maxtry=1

,6while

i<

maxtry 

andupdatesystime()

==0:

printu'

網路超時,第%i次重試'%

ii=i

+1ifi

<

maxtry:

printu'

網路校時成功:

'printcurtime()

else

:printu'

\r\n網路校時失敗,請檢查網路!'

因為設定了超時時間僅為0.3秒,所以我想無論如何這樣的時間精度都是可以接受的。實際上,在我的測試中即使設定timeout=0.01的情況下也沒有出現過超時錯誤----當然,我是在北京試的。

Ubuntu12 10 校時服務(NTP)

1 安裝ntp sudo apt get install ntp 2 ntp服務端配置檔案位置 etc ntp.conf 3 ntp服務端配置檔案引數說明 restrict 4 default kod nomodify notrap nopeer noquery restrict 6 default...

根據阿里NTP校時本地時間

import ntplib import os,datetime defntp client hosts 0.cn.pool.ntp.org 1.cn.pool.ntp.org 2.cn.pool.ntp.org 3.cn.pool.ntp.org hosts ntp.aliyun.com ntp1...

網路自動校時器

if strtotime 06 01 01 strtotime timetostr time then 呼叫 begin timer1.enabled false verifystandardtime 校驗標準時間 timer1.enabled true end procedure tmyservi...