Board logo

标题: [技术讨论] ubuntu同步网站时间(python3) [打印本页]

作者: Gin_Q    时间: 2020-7-24 17:23     标题: ubuntu同步网站时间(python3)

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

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





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2