开发者

Easy way to get the correct time in python

开发者 https://www.devze.com 2023-02-15 03:29 出处:网络
I\'m running some jobs on a cluster where the dates on each node are slightly off from one another. Is there an easy way to get the time from somewhere on the internet via python or do I need to have

I'm running some jobs on a cluster where the dates on each node are slightly off from one another. Is there an easy way to get the time from somewhere on the internet via python or do I need to have the sysadmin sync the times between the machines more frequently?

I'd like to put a time-stamp in the output from each job I launch, but o开发者_开发问答bviously the accuracy of the python's time.strftime() etc are going to depend on the machine knowing the correct time. I'd love accuracy within a few seconds, but right now it's within minutes.


I'm dealing with a standalone sensor device that runs Linux 2.6.33, Python 2.6.5, and unfortunately lacks a real-time clock but does have networking capabilities. Additionally, the device uses BusyBox so it has a minimalized set of tool capabilities.

I created the script below that executes after the network is running to correct the system time. My solution might not exactly address the original question (sounds like Noah is a user and not an admin on the system), but this question is what comes when searching for sync'ing system clock using Python via NTP. I figure it might help others that land here.

Before you use the script, you'll need to install ntplib. The download link and installation instructions are here: https://pypi.python.org/pypi/ntplib/.

Copy the the contents of the code below to a file (I named mine "synctime.py"). Then, after the network is running, execute the script (e.g. "python synctime.py"). You will need to do this using elevated (root) privileges for this to work properly.

import time
import os

try:
    import ntplib
    client = ntplib.NTPClient()
    response = client.request('pool.ntp.org')
    os.system('date ' + time.strftime('%m%d%H%M%Y.%S',time.localtime(response.tx_time)))
except:
    print('Could not sync with time server.')

print('Done.')


Sounds like you should look into using something like NTP to keep the machines' time in sync with one another (and preferably, a global standard time source).


Try ntplib for python. I've played around with it a bit and it seems pretty stable.

https://pypi.python.org/pypi/ntplib/


-->Run "w32tm /resync" on cmd.

this dosnt not work in my case.

which work for me:

        client = ntplib.NTPClient()
        response = client.request('pool.ntp.org')
        t = datetime.fromtimestamp(response.tx_time)
        time_ntp = t.strftime("%m %d %H:%M:%S %Y")#Mon Jul 05 13:58:39 2021
        print('NTP Time='+str(time_ntp))
        #os.system('w32tm /resync')
        os.system('date ' +  t.strftime("%x"))
        os.system('time ' +  t.strftime("%X"))

-praveen


Run "w32tm /resync" on cmd.

import os

os.system('w32tm /resync')
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号