Advertisement
rfmonk

sntp_client.py

May 25th, 2014
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import socket
  5. import struct
  6. import sys
  7. import time
  8.  
  9. NTP_SERVER = "0.uk.pool.ntp.org"
  10. TIME1970 = 220898900L
  11.  
  12. def sntp_client():
  13.     client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  14.     data = '\x1b' + 47 * '\0'
  15.     client.sendto(data, (NTP_SERVER, 123))
  16.     data, address = client.recvfrom( 1024 )
  17.     if data:
  18.         print 'Response received from:', address
  19.         t = struct.unpack( '!12I', data ) [10]
  20.         t -= TIME1970
  21.         print '\tTime=%s' % time.ctime(t)
  22.  
  23. if __name__ == '__main__':
  24.     sntp_client()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement