返回列表 发帖

[技术讨论] ubuntu同步网站时间(python3)

本帖最后由 Gin_Q 于 2020-7-25 10:37 编辑

# date -u -s
# header('Date')
# conversion
#!/usr/bin/env python3
#coding:utf-8
import urllib.request
import time
import os
url = 'https://www.baidu.com/'
lc_time = time.strftime("%Y/%m/%d %H:%M:%S %z")
try:
    web = urllib.request.urlopen(url,timeout=1)
except BaseException:
    print('\nFailed to access the network {}\n'.format(url))
    exit()
t = time.strptime(web.getheader('Date'), "%a, %d %b %Y %H:%M:%S GMT")
t1 = time.localtime(time.mktime(t))
t2 = '{}-{}-{} {}:{}:{}'.format(t1.tm_year,t1.tm_mon,t1.tm_mday,t1.tm_hour,t1.tm_min,t1.tm_sec)
os.system('date -u -s ' + '"' + t2 + '"')
os.system('echo "\033[36m`date +"%Y-%m-%d %H:%M:%S %z"`\033[0m"')COPY

返回列表