标题: [技术讨论] ubuntu同步网站时间(python3) [打印本页]
作者: Gin_Q 时间: 2020-7-24 17:23 标题: 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"')
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |