Learning Python Network Programming

(Sean Pound) #1
Chapter 6

To create an NTP client, you need to call the ntplib's NTPCLient class.


import ntplib


from time import ctime


c = ntplib.NTPClient()


response = c.request('pool.ntp.org')


print ctime(response.tx_time)


Here, we have selected pool.ntp.org, which is a load-balanced webserver.
So, a pool of the NTP servers will be ready to respond to the client's request.
Let's find more information regarding this from the response that was returned
by an NTP server.


import ntplib
from time import ctime

HOST_NAME = 'pool.ntp.org'

if __name__ == '__main__':
params = {}
client = ntplib.NTPClient()
response = client.request(HOST_NAME)
print('Received time: %s' %ctime(response.tx_time))
print('ref_clock: ',ntplib.ref_id_to_text(response.ref_id,
response.stratum))
print('stratum: ',response.stratum)
print('last_update: ', response.ref_time)
print('offset: %f' %response.offset)
print('precision: ', response.precision)
print('root_delay: %.6f' %response.root_delay)
print('root_dispersion: %.6f' %response.root_dispersion)

The detailed response will look like the following:


$ python 6_5_ntp_client.py


Received time: Sat Feb 28 17:08:29 2015


ref_clock: 213.136.0.252


stratum: 2


last_update: 1425142998.2


offset: -4.777519


precision: -23


root_delay: 0.019608


root_dispersion: 0.036987

Free download pdf