-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathntp.py
More file actions
30 lines (30 loc) · 1.34 KB
/
Copy pathntp.py
File metadata and controls
30 lines (30 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import ntplib
from time import ctime
client = ntplib.NTPClient()
# Use of pool.ntp.org from inside your organization may be inappropriate
# or or it may be inaccessible (NTP may be blocked at your perimeter).
# Your organization may have an "internal" NTP server. If so, replace
# "pool.ntp.org" with your trusted server.
ntpserver = 'pool.ntp.org'
try:
response = client.request(ntpserver, version=3)
except (Exception, ntplib.NTPException) as e:
print(f"ntplib error occured while querying the server. {e}")
except Exception as ex:
print(f"Unexpected error occured while querying the server. {ex}")
else:
print('{0} {1} - ntp stratum: {2}, precision: {3} seconds'.format(ctime(response.tx_time), ntpserver, response.stratum, (response.precision*.001)))
#
# or use this as a function and return a timestamp as a string
#
# The options below might be useful for narrow problem-solving use cases:
# print(ctime(response.tx_time))
# print('ntp version: %s' % response.version)
# print('ntp mode: %s' % response.mode)
# print('round-trip delay: %s' % response.delay)
# print('root delay: %s' % response.root_delay)
# print('ntp precision: %s' % response.precision)
# print('ntp stratum: %s' % response.stratum)
# print('ntp root_delay: %s' % response.root_delay)
# print('ntp root_dispersion: %s' % response.root_dispersion)
# print(ctime(response.tx_time))