timesync with python?

Fredrik Lundh fredrik at pythonware.com
Thu Sep 9 08:01:50 EDT 1999


Holger Jannsen <holger at phoenix-edv.netzservice.de> wrote:
> But I also thought that someone has an idea about that before
> and did it likely better than me. So, do you know a weblink
> to a script which already nearly does what I need? Perhaps 
> directly with interrogation at port 37 NTP?

from the eff-bot archives:

import socket, time

HOST = "my.server"
PORT = 37

TIME1970 = 2208988800L # 1970-01-01 00:00:00

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(HOST, PORT)

b = s.recv(4)

t0 = ord(b[3]) + (ord(b[2])<<8) + (ord(b[1])<<16) + (ord(b[0])<<24L)
t1 = int(time.time()) + TIME1970

dt = int(t0 - t1)

print "delta is", dt, "seconds"





More information about the Python-list mailing list