The laptop at hand is long, and the CMOS battery may be out of power. Each boot time is not correct, resulting in access to some HTTPS Web pages will appear warning message.
So looking for a Python script to set the system time method, found two, one is to call the socket directly send UDP packets to the National Service Center, and the other is to call the Ntplib package. I ping the local computer National Time Service Center address cn.pool.ntp.org sometimes dropped packets, however, both do not check whether UDP packet loss mechanism, method one after the UDP packet has been blocking state can not exit, method Two although will prompt time out, but no longer do other attempts, such as a re-contract, or to the same domain name of different IP address of the contract. As a result, an attempt was made to add a timeout mechanism to the code based on method one, and attempt to contract a different IP address for the same domain name.
The complete code for the specific changes is as follows:
#-*-coding:utf-8-*-ImportSocketImportstructImport TimeImportWin32APIImportOSImportRedefGetTime (timeserveraddresses): time_1970= 2208988800LClient=Socket.socket (socket.af_inet, socket. SOCK_DGRAM) Client.settimeout (3) Data='\x1b'+ 47 *' /' #timeserver_ip=socket.gethostbyname (' cn.pool.ntp.org ') #timeserver_ip= ' 202.118.1.130 'Port=123 forAddressinchtimeserveraddresses:success=False Count=0 while notSuccess andCount<3: PrintAddress,countTry: Client.sendto (data, (address, Port)) data, address= Client.recvfrom (1024) Success=Trueexceptsocket.timeout:Print 'Request timed out!'Count=count+1ifsuccess==true:a BreakData_result= Struct.unpack ('!12i', data) [10] Data_result-=time_1970returnData_resultdefSetSystemTime (now_time): Tm_year, Tm_mon, Tm_mday, Tm_hour, Tm_min, Tm_sec, Tm_wday, Tm_yday, Tm_isdst=time.gmtime (now_time) Win32API. SetSystemTime (Tm_year, Tm_mon, Tm_wday, Tm_mday, Tm_hour, Tm_min, tm_sec, 0)Print "Set System ok!"defGetserverip ():res1=os.popen ('nslookup cn.pool.ntp.org') Result1=Res1.read () addresses=result1.split ('\ n') [1].split ('\ n') [1][12:].split (',') returnaddresses#For address in addresses: #Res=os.popen (' Ping-n 2 ' +address) #Result=res.read () #Received_num=int (Re.findall (' received = [0-9] ', result) [0].split (' = ') [1]) #if Received_num > 1: # Break #timeserver=addressif __name__=='__main__': Addresses=Getserverip () now_time=GetTime (Addresses) setsystemtime (now_time)Print "%d-%d-%d%d:%d:%d"% Time.localtime (now_time) [: 6]
Reference:
[1] method one: Using Python to set the system time (to the system)-Sea Sailor-blog channel-csdn.net
[2] Method two: Python third-Party library recommendations-sync time on Windows via Ntplib-mattkang-Blog channel-csdn.net
[3]ntplib Source: https://pypi.python.org/pypi/ntplib/
[4]socket setting timeout: Attributes-python socket timeout Error-stack Overflow
[5] Python Socket API notes-my2010sam's column-Blog channel-csdn.net
[6]python Execution System command: Os.system (), Subprocess.popen (), commands-Poplar log-NetEase Blog
[7]windows Installation Python:http://my.oschina.net/bailiangcn/blog/288538#osc_h2_2
[8] National Service Center IP 210.72.145.44 not available? -Open source China community
Python has timed-out windows system time-setting code