Skip to content

Instantly share code, notes, and snippets.

@plopp
Created July 8, 2016 16:32
Show Gist options
  • Save plopp/755cbdb0420403c9df60a2be108dc010 to your computer and use it in GitHub Desktop.
Save plopp/755cbdb0420403c9df60a2be108dc010 to your computer and use it in GitHub Desktop.
Calls an NTP server and converts the response to a millisecond unix epoch timestamp
import socket
import struct
import time
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
data = '\x1b' + 47 * '\0'
client.sendto(data, ("0.se.pool.ntp.org",123))
data, address = client.recvfrom( 1024 )
if data:
utc_secs = struct.unpack('!12I', data)[10]
utc_millis = struct.unpack('!12I', data)[11]/4294967296.0
utc_secs -= 2208988800L
print int((utc_secs+utc_millis)*1000.0)
client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment