#电脑时间用了一段时间后,系统时间不准了,想更新一下
#在windows里面,更新系统时间,时常失败,而且速度很忙.
#在网上拷贝的代码,发现很好用,比windows自带的实现要快.
#-*- coding:utf-8 -*-
import socket
import struct
import time
import win32api
TimeServer = '210.72.145.44' #国家授时中心ip
Port = 123
def getTime():
TIME_1970 = 2208988800L
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
data = '\x1b' + 47 * '\0'
client.sendto(data, (TimeServer, Port))
data, address = client.recvfrom(1024)
data_result = struct.unpack('!12I', 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, 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.localtime(getTime())[:6]
easy_install ntplib或pip install ntplib
import os
import time
import ntplib
c = ntplib.NTPClient()
response = c.request('pool.ntp.org')
ts = response.tx_time
_date = time.strftime('%Y-%m-%d',time.localtime(ts))
_time = time.strftime('%X',time.localtime(ts))
os.system('date {} && time {}'.format(_date,_time))
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有