Last active
August 29, 2015 14:11
-
-
Save tlhc/57ce789ae633156c6bac to your computer and use it in GitHub Desktop.
ntp request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from socket import AF_INET, SOCK_DGRAM | |
import sys | |
import socket | |
import struct, time | |
def getNTPTime(host = "pool.ntp.org"): | |
port = 123 | |
buf = 1024 | |
address = (host,port) | |
msg = '\x1b' + 47 * '\0' | |
# reference time (in seconds since 1900-01-01 00:00:00) | |
TIME1970 = 2208988800L # 1970-01-01 00:00:00 | |
# connect to server | |
client = socket.socket( AF_INET, SOCK_DGRAM) | |
client.sendto(msg, address) | |
msg, address = client.recvfrom( buf ) | |
t = struct.unpack( "!12I", msg )[10] | |
t -= TIME1970 | |
return time.ctime(t).replace(" "," ") | |
if __name__ == "__main__": | |
print getNTPTime() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment