Skip to content

Instantly share code, notes, and snippets.

@sbp
Created December 31, 2011 23:58
Show Gist options
  • Save sbp/1545701 to your computer and use it in GitHub Desktop.
Save sbp/1545701 to your computer and use it in GitHub Desktop.
Clock script
import re, math, time, urllib, locale, socket, struct, datetime
from decimal import Decimal as dec
while True:
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.sendto('\x1b' + 47 * '\0', ('ntp1.npl.co.uk', 123))
data, address = client.recvfrom(1024)
if data:
buf = struct.unpack('B' * 48, data)
d = dec('0.0')
for i in range(8):
d += dec(buf[32 + i]) * dec(str(math.pow(2, (3 - i) * 8)))
d -= dec(2208988800L)
a, b = str(d).split('.')
f = '%Y-%m-%d %H:%M:%S'
result = datetime.datetime.fromtimestamp(d).strftime(f) + '.' + b[:6]
print result + ' - ntp1.npl.co.uk'
time.sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment