python3.7 获取网络时间_python 获取网络时间-CSDN博客
# -*- coding: utf-8 -*- import datetime import time import ntplib import requests def get_beijing_time(): try: url = 'https://beijing-time.org/' request_result = requests.get(url=url) if request_result.status_code == 200: headers = request_result.headers net_date = headers.get("date") gmt_time = time.strptime(net_date[5:25], "%d %b %Y %H:%M:%S") bj_timestamp = int(time.mktime(gmt_time) + 8 * 60 * 60) return datetime.datetime.fromtimestamp(bj_timestamp) except Exception as exc: return datetime.datetime.now() def get_ntp_time(): ntp_client = ntplib.NTPClient() response = ntp_client.request('pool.ntp.org') return datetime.datetime.fromtimestamp(response.tx_time) if __name__ == '__main__': bj_time = get_beijing_time() print("北京时间: ", bj_time) ntp_time = get_ntp_time() print("ntp时间: ", ntp_time)
标签:__,在线,get,Python,ntp,datetime,日期,time,import From: https://www.cnblogs.com/daizichuan/p/17799689.html