You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.3 KiB

import json
import time
import requests
class JDTimer:
def __init__(self):
self.headers = {
'user-agent': 'okhttp/3.12.1;jdmall;android;version/10.5.0;build/95837;',
'content-type': 'application/x-www-form-urlencoded',
}
self.session = requests.Session()
try:
self.jd_time()
except Exception as e:
print(e)
def jd_time(self):
"""
从京东服务器获取时间毫秒
:return:
"""
url = 'https://api.m.jd.com/client.action?functionId=queryMaterialProducts&client=wh5'
ret = self.session.get(url, headers=self.headers, allow_redirects=False, verify=False).text
js = json.loads(ret)
return int(js["currentTime2"])
def local_time(self):
"""
获取本地毫秒时间
:return:
"""
return int(round(time.time() * 1000))
def local_jd_time_diff(self):
"""
计算本地与京东服务器时间差
:return:
"""
try:
return self.local_time() - self.jd_time()
except Exception as e:
print(e)
return 0
if __name__ == '__main__':
jdtimer = JDTimer()
for i in range(5):
print(jdtimer.local_jd_time_diff())