Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. import socket
  2. import struct, sys, time
  3.  
  4. # default server
  5. host = "localhost"
  6. port = 8037
  7.  
  8. # reference time (in seconds since 1900-01-01 00:00:00)
  9. TIME1970 = 2208988800L # 1970-01-01 00:00:00
  10.  
  11. def gettime(host, port):
  12. # fetch time buffer from stream server
  13. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  14. s.connect((host, port))
  15. t = s.recv(4)
  16. s.close()
  17. t = struct.unpack("!I", t)[0]
  18. return int(t - TIME1970)
  19.  
  20. if __name__ == "__main__":
  21. # command line utility
  22. if sys.argv[1:]:
  23. host = sys.argv[1]
  24. if sys.argv[2:]:
  25. port = int(sys.argv[2])
  26. else:
  27. port = 37 # default for public servers
  28.  
  29. t = gettime(host, port)
  30. print "server time is", time.ctime(t)
  31. print "local clock is", int(time.time()) - t, "seconds off"
  32.  
  33. ## server time is Sat Oct 09 16:58:50 1999
  34. ## local clock is 0 seconds off
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement