Uses the SNTP protocol (as specified in RFC 2030) to contact the server specified in the command line and report the time as returned by that server. This is about the simplest (and dumbest) client I could manage.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
from socket import * import struct import sys import time TIME1970 = 2208988800L # Thanks to F.Lundh client = socket( AF_INET, SOCK_DGRAM ) data = '\x1b' + 47 * '\0' client.sendto( data, ( sys.argv, 123 )) data, address = client.recvfrom( 1024 ) if data: print 'Response received from:', address t = struct.unpack( '!12I', data ) t -= TIME1970 print '\tTime=%s' % time.ctime(t)
I would like too see a more complete implementation of SNTP or even full-blown NTP in Python. Anyone interested? I might even try it myself.