You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
12306/config/AutoSynchroTime.py

39 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# coding=utf-8
import os
import platform
import ntplib
import datetime
def autoSynchroTime():
"""
同步北京时间执行时候请务必用sudosudosudo 执行否则会报权限错误windows打开ide或者cmd请用管理员身份
:return:
"""
c = ntplib.NTPClient()
hosts = ['ntp1.aliyun.com', 'ntp2.aliyun.com', 'ntp3.aliyun.com', 'ntp4.aliyun.com', 'cn.pool.ntp.org']
print(u"正在同步时间请耐心等待30秒左右如果下面有错误发送可以忽略")
print(u"系统当前时间{}".format(str(datetime.datetime.now())[:22]))
system = platform.system()
if system == "Windows": # windows 同步时间未测试过参考地址https://www.jianshu.com/p/92ec15da6cc3
for host in hosts:
os.popen('w32tm /register')
os.popen('net start w32time')
os.popen('w32tm /config /manualpeerlist:"{}" /syncfromflags:manual /reliable:yes /update'.format(host))
os.popen('ping -n 3 127.0.0.1 >nul')
sin = os.popen('w32tm /resync')
if sin is 0:
break
else: # mac同步地址如果ntpdate未安装brew install ntpdate linux 安装 yum install -y ntpdate
for host in hosts:
sin = os.popen('ntpdate {}'.format(host))
if sin is 0:
break
print(u"同步后时间:{}".format(str(datetime.datetime.now())[:22]))
if __name__ == '__main__':
autoSynchroTime()