UDP Connectionless Example: time.nist.gov : UDP « Network « Python






UDP Connectionless Example: time.nist.gov

 
import socket, sys, struct, time

hostname = 'time.nist.gov'
port = 37

host = socket.gethostbyname(hostname)

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto('', (host, port))

print "Looking for replies; press Ctrl-C to stop."
buf = s.recvfrom(2048)[0]
if len(buf) != 4:
    print "Wrong-sized reply %d: %s" % (len(buf), buf)
    sys.exit(1)

secs = struct.unpack("!I", buf)[0]
secs -= 2208988800
print time.ctime(int(secs))


           
         
  








Related examples in the same category

1.UDP Example
2.A UDP Uppercase Server
3.UDP Broadcast Server