Easy way to get the right time in python

I am running some tasks in a cluster where the dates on each node are slightly separated from each other. Is there an easy way to get time from somewhere on the internet via python or do I need sysadmin to synchronize time between machines more often?

I want to put a timestamp in the results of each task run, but obviously the accuracy of python time.strftime() , etc. will depend on the car, knowing the correct time. I would like to get accuracy in a few seconds, but right now in a few minutes.

+4
source share
3 answers

Try ntplib for python. I played a little with him, and he looks pretty stable.

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

+3
source

I am dealing with a standalone touch device that runs on Linux 2.6.33, Python 2.6.5 and, unfortunately, does not have a real-time clock, but has network capabilities. In addition, the device uses BusyBox, so it has a minimal set of tool capabilities.

I created a script below that is executed after the network is running to fix the system time. My solution may not accurately address the original question (it sounds like Noah is the user, not the system administrator), but this question arises when searching for synchronizing system clocks using Python via NTP. I suppose this can help others who land here.

Before using the script, you need to install ntplib. Download and installation instructions are here: https://pypi.python.org/pypi/ntplib/ .

Copy the contents of the code below to the file (I named my "synctime.py"). Then, after starting the network, execute the script (for example, "python synctime.py"). To do this, you will need to do this using privileges with privileges (root).

 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.') 
+7
source

It sounds like you should learn something like NTP to synchronize the time between machines (and preferably the global standard time source).

+5
source

Source: https://habr.com/ru/post/1342672/


All Articles