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