Home > Software engineering >  Python writing method of modify the operating system time, run time cannot modify system time, where
Python writing method of modify the operating system time, run time cannot modify system time, where

Time:09-27

# - * - coding: utf-8 - * -
The import socket
The import struct
The import time
The import win32api
TimeServer='210.72.145.44' # national center for timing IP
Port=123
Def getTime () :
TIME_1970=2208988800 l
Client=socket. The socket (socket. AF_INET, socket. SOCK_DGRAM)
47 * data='https://bbs.csdn.net/x1b' + '\ 0'
Client. Sendto (data, (TimeServer, Port))
Data, address=client. Recvfrom (1024)
Data_result=struct. Unpack ('! 12 I ", data) [10]
Data_result -=TIME_1970
Return data_result
Def setSystemTime () :
Tm_year tm_mon, tm_mday, tm_hour tm_min, tm_sec, tm_wday and tm_yday, tm_isdst=time. Gmtime (getTime ())
Win32api. SetSystemTime (tm_year, tm_mon tm_wday, tm_mday, tm_hour, tm_min, tm_sec, 0)
Print "Set System OK!"
If __name__=="__main__ ':
SetSystemTime ()
Print "% d % d - % d % d: % d: % d" % time. The localtime (getTime ()) 6] [:

Put the above procedures in. Py format file, CMD window control execution of python test. Py enter, why the next line cursor blink, then there is no any response,
Change the IP address to cn.pool.ntp.org, the implementation of the python test. Py enter, quote us the following prompt: pywintypes. Error: (1314, 'SetSystemTime', '\ XBF \ XCD \ XBB \ xa7 \ xb6 \ XCB \ xc3 \ XBB \ xd3 \ xd0 \ XCB \ xf9 \ xd0 \ xe8 \ xb5 \ xc4 \ XCC \ xd8 \ xc8 \ xa8 \ xa1 \ xa3'),
The system time is not updated, please everyone a great god show

CodePudding user response:

Permissions or group policy setting problem, I guess,