How to do it...

Synchronizing your time is crucial accounting task. While it is not ideal to depend on the time from the internet for mission-critical systems, the NTP time is sufficiently accurate for most of the computing requirements.

Listing 11.5 gives a simple NTP server connection test as follows:

#!/usr/bin/env python 
# Python Network Programming Cookbook, Second Edition 
-- Chapter - 11 # 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 argparse import ntplib from time import ctime def main(address, v): c = ntplib.NTPClient() response = c.request(address, version=v) print("Response Offset: ", response.offset) print("Version: ", response.version) print("Response (Time): ", ctime(response.tx_time)) print("Leap: ", ntplib.leap_to_text(response.leap)) print("Root Delay: ", response.root_delay) print(ntplib.ref_id_to_text(response.ref_id)) if __name__ == '__main__': parser = argparse.ArgumentParser(description='Query NTP Server') parser.add_argument('--address', action="store",
dest="address", default='pool.ntp.org') parser.add_argument('--version', action="store",
dest="version", type=int, default=3) given_args = parser.parse_args() address = given_args.address version = given_args.version main (address, version)

In this recipe, we provide an NTP server to query the time. Running this program provides the following outcome:

$  python 11_5_query_ntp_server.py --address=europe.pool.ntp.org --version=3
('Response Offset: ', -0.002687215805053711)
('Version: ', 3)
('Response (Time): ', 'Mon Jul 24 23:06:10 2017')
('Leap: ', 'no warning')
('Root Delay: ', 0.00372314453125)
10.176.63.115
    
This recipe is similar to the  standard NTP query program ntpq.
$ ntpq -pn
remote           refid      st t when poll reach   delay   offset  jitter
=========================================================
0.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
1.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
2.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
3.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
ntp.ubuntu.com  .POOL.          16 p    -   64    0    0.000    0.000   0.000
-91.189.91.157   132.246.11.231   2 u  257  512  377   94.363   -3.998  13.016
+193.190.147.153 193.190.230.65   2 u  184  512  377   10.327   -2.246   1.235
+91.189.89.198   17.253.52.125    2 u  169  512  377   14.206   -2.744   1.414
*94.143.184.140  .GPS.            1 u  279  512  377   14.208   -2.743   2.453
+213.189.188.3   193.190.230.65   2 u    1  512  377   11.015   -2.192   1.569
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.5.201