import usocket as socket import ustruct as struct # (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60 NTP_DELTA = 3155673600 NTP_PKT_LEN = 48 NTP_QUERY = '\x1b' + (NTP_PKT_LEN-1) * '\0' def get_ntp_time(host = "pool.ntp.org"): addr = socket.getaddrinfo(host, 123)[0][-1] s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.SO_REUSEADDR) s.settimeout(2) # sending twice - it appears the first packet after connecting to the network does not get sent s.sendto(NTP_QUERY, addr) s.sendto(NTP_QUERY, addr) msg = s.recv(NTP_PKT_LEN) del s val = struct.unpack("!I", msg[40:44])[0] return val - NTP_DELTA