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[1], 123 ))
data, address = client.recvfrom( 1024 )
if data:
print 'Response received from:', address
t = struct.unpack( '!12I', data )[10]
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.
Tags: network
from socket import *. Nice job in a few lines of code. I'd like to see the namespace-polluting import line replaced, especially in published Python code.
Done as requested. I can't login to change this code having lost my account details but the codes so short I can just post it here.
Thank you Simon Foster, this recipe helped me a lot. Up to now I used sntp with subprocess, but this brings me this error very often:
sntp: unable to format current local time
I'm having a spot of bother with this script, in Python 3.4.2.
The line in question including the error message looks like:
I'm not sure how to correctly format the sendto line, and the data line looks suspicious also.