一个简单的SNTP客户端

借鉴于python网络编程攻略

#/usr/local/bin/python3.5
#coding:utf-8 import socket, struct, time NTP_server = "0.uk.pool.ntp.org"
TIME1970 = 2208988800 def sntp_client():
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
data = "\x1b" + 47 * "\0"
client.sendto(data.encode('utf-8'), (NTP_server, 123))
data, address = client.recvfrom(1024) if data:
print("Response received from:", address)
t = struct.unpack('!12I', data)[10]
t -= TIME1970
print('\tTime=%s' % time.ctime(t)) sntp_client()

效果图如下

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjgAAAA9CAIAAAAvRYQ7AAAKtElEQVR4nO2dS5rrKAyFsy4vyOvxEnoVXsXdgUd30kvoqXuQxOEhIQkEplLn/2qUECSeBwRlP/7++x/59+fPnxMAAAC4m4coVP8AAAAA9yELFQAAAHAjECoAAABTA6ECAAAwNRAqAAAAUwOhAgAAMDUQKnCe53ke2/J4PB7LdtztCXhzbAvaA0zLyP4JoQLneenUcKXa10abl+Mv1t3Ruzt5F4wpEBYWP4svbK+hRbIIVTonfNW88Nu5aSC1CdW+fmuHfDZHoTBSe6WDlUoYV199zWnzefvEOR373NwVr+wqS5b0rvKKQXJ65PiS6lnRXrqyv9MNGHStQjWq7sF30iJUr/74JdIUohn+zzRk1TEDNUxMJ7E3hDKfeN6jzNAZNbRtYLImF2INlOdkcbrQXn6I9axqL1XZ48TdB6FdqAKXhOgEABItQrWv39n3xN3UeZ6lCYL6Jpsmj22hhMtcnYp8rnlv3Z9f5+1N7DfaliGvOthUVclkEP2McCcv67uk2lbxRVHPunZXlD2z23u70iRU748yJ2PdLq0wOPG/JqEoK74D9M5HXy6BY1veuQcWxbUPZ0tOJaQQIxfcAKO6p8ZnYk3XQ6jU9Vxq9319PJbtiEIp7/R8kzVORspxLwgVOxDs+dkg8vlMfMX5IrWtE2zWh2U7GrJgMs2W6WQ9UxYH7DzEemZ+JPtV7j2etczhL1Sa7TC5t0z79uOxLMKhGL1HDd3h8s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MmsE4UlavSVNuQyVKiiKW/ZjqYFdznMWN4e6JFe5epywUFZP643DJMS5PFMS3uV49EUiAb6A6HqQbNQSa8/dwpuBbbii+rpF9wVglioklR1QiXvqNRHA+OEipikmozTtvIs/Uqo+qeEmrdR1NZP+5sv6C292R9JqKjLi9hWeQOh6sEUQqVY8YURvnzPMlyolGdUcwkVd77RdNJO2ir+A4RDQTXVZb8jUl8/NfdRkh8X7miq/SkKlfpj0AaEqgdjhEowIAsV72Z2Xi/dIXARKm52SVwQ727XOyDnpr2S2DTLfpNQ1ddPdRWWz+as/mjDsGFG2FF5A6HqQXeh8gkx8G6G3xBBofwjH6Eq2QoyJz4qXKbwWt6y+sjURothrRY7Xk9X9KhSsQq2quqnugrlchj9kdo9D8VCp/yBUPVA2KoUl8M6oWJzsoxsnVCpDrqdhIrdNiS5kKlyS0Qy6xzCNlhYWDJR9RxryKr6err8DweKf4BQ2tLUj8VWCd0NB0V7qdqC9hpxvw5AqHowQqiY3LpcB89NkZETD6H6/JC1Rabiyt16X1wtHlHCurnKWahK63vpwl/wc13diXuJcv2YbBXQX8UT2quu3bGX6gWECgAAwNRAqAAAAEwNhAoAAMDUQKgAAABMDYQKAADA1GiF6vF43OUiAACA3wyECgAAwNRAqAAAAEwNhAoAAMDUQKgAAABMDYQKAADA1ECoAAAATA2ECgAAwNRAqAAAAEwNhAoAAMDU/A/oN1RYZJLnpgAAAABJRU5ErkJggg==" alt="" />

END!

上一篇:c++模板使用及实现模板声明定义的分离


下一篇:cvKMeans2函数用法概述