January 2019
Beginner to intermediate
776 pages
19h 58m
English
We create an instance of NTPClient and then we call the request() method on it by passing the NTP server address.
Listing 1.11 shows how to print the current time from the internet time server as follows:
#!/usr/bin/env python
# This program is optimized for Python 2.7.12 and Python 3.5.2.
# It may run on any other version with/without modifications.
import ntplib
from time import ctime
def print_time():
ntp_client = ntplib.NTPClient()
response = ntp_client.request('pool.ntp.org')
print (ctime(response.tx_time))
if __name__ == '__main__':
print_time()
In my machine, this recipe shows the following output:
$ python 11_11_print_machine_time.py Fri Jun 2 16:01:35 2017
Read now
Unlock full access