-
Notifications
You must be signed in to change notification settings - Fork 2
/
netTime.py
42 lines (37 loc) · 1.34 KB
/
netTime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 29 10:03:14 2017
@author: xiaoniu29
"""
import time,ctypes
import requests
SetSystemTime = ctypes.windll.kernel32.SetSystemTime
class SYSTEMTIME(ctypes.Structure):
c_ushort= ctypes.c_ushort
_fields_ = (
('wYear', c_ushort),
('wMonth', c_ushort),
('wDayOfWeek', c_ushort),
('wDay', c_ushort),
('wHour', c_ushort),
('wMinute', c_ushort),
('wSecond', c_ushort),
('wMilliseconds', c_ushort),
)
def __str__(self):
return '%4d%02d%02d%02d%02d%02d.%03d' % (self.wYear,self.wMonth,self.wDay,self.wHour,self.wMinute,self.wSecond,self.wMilliseconds)
def updateSystemTime(url= 'http://www.jd.com'):
print('Domain:',url)
try:
response= requests.get(url)
date= response.headers['date']
gmt=time.strptime(date[5:25], "%d %b %Y %H:%M:%S")
st=SYSTEMTIME(gmt.tm_year,gmt.tm_mon,gmt.tm_wday,gmt.tm_mday,gmt.tm_hour,gmt.tm_min,gmt.tm_sec,0)
SetSystemTime(ctypes.byref(st))
print('网络校时成功!')
except Exception as ex:
print(ex)
print('网络校时失败!')
return False
return True
updateSystemTime('https://hi.taobao.com')