# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@des :
"""
import requests
from lxml import etree
import re
import datetime
def get_standard_html(retry_times=3):
"""
网络获取标准北京时间HTML
:param retry_times:
:return:
"""
flag = False
response = ""
while not flag and retry_times > 0:
try:
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"DNT": "1",
"Pragma": "no-cache",
"Referer": "https://www.baidu.com/link?url=P5aQMA4XN6hQMNXgWTAKtPCSGG52KlhztMU78ERAT3KS3pixUFfh2loQ4dUgCQZ0&wd=&eqid=d2bcf4ac0001cb160000000263295be3",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
}
url = "http://time.tianqi.com/"
response = requests.get(url, headers=headers, verify=False)
comp = re.compile("charset=(.*)")
encod = re.search(comp, response.text)
response.encoding = encod
flag = True
except Exception as e:
retry_times -= 1
return response
def parse_text(res):
"""
解析标准北京时间
:param res:
:return:
"""
tree = etree.HTML(res)
lis = tree.xpath('//div[@class="time_box_detailinfo"]')
for li in lis:
net_time_lis = li.xpath('./div[@class="time_date"]/div[1]/p/text()')
computer_time = li.xpath(
'./div[@class="detail_info"]/p[5]/span[@class="computed_time"]/text()')
if len(computer_time) == 1:
computer_time = computer_time[0]
net_time_head = computer_time.split(" ")[0]
if len(net_time_lis) == 3:
net_time = "{} {}:{}:{}".format(
net_time_head, net_time_lis[0], net_time_lis[1], net_time_lis[2])
return net_time, computer_time
def time_str_datatime_obj(time_str: str):
"""
字符串形式时间转换成datetime.datetime
:param time_str:
:return:
"""
return datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
def match_time(register_time: str, net_time_now: str,
local_time=datetime.datetime.now(),TrialTime=30):
"""
计算时间差
:param register_time:
:param net_time_now:
:param local_time:
:return:
"""
# 校验网络时间和本地时间
if local_time.strftime("%Y-%m-%d %H:%M:%S") == net_time_now:
register_time_obj = time_str_datatime_obj(register_time)
net_time_now_obj = time_str_datatime_obj(net_time_now)
# 计算时间差值
diff_days_net = (net_time_now_obj - register_time_obj).days
if diff_days_net > TrialTime:
flag = 2
elif diff_days_net < 0:
flag = 3
else:
flag = 0
else:
flag = 4
return flag
if __name__ == '__main__':
match_dic = {
0: "试用期内",
2: "试用过期",
3: "注册时间大于当前时间",
4: "网络时间获取异常/本地时间不正确"
}
res_txt = get_standard_html()
if res_txt != "":
net_time, computer_time = parse_text(res_txt.text)
register_time = "2022-7-01 19:01:00"
status_code = match_time(register_time, net_time)
print(match_dic[status_code])
else:
print("网络错误")
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/156891.html