Ich habe einen NTP-Client in Python geschrieben, um einen Zeitserver abzufragen und die Zeit anzuzeigen, und das Programm wird ausgeführt, aber gibt mir keine Ergebnisse. Ich verwende die Python 2.7.3 integrierte Entwicklungsumgebung und mein OS ist Windows 7. Hier ist der Code:NTP-Client in Python
# File: Ntpclient.py
from socket import AF_INET, SOCK_DGRAM
import sys
import socket
import struct, time
# # Set the socket parameters
host = "pool.ntp.org"
port = 123
buf = 1024
address = (host,port)
msg = 'time'
# reference time (in seconds since 1900-01-01 00:00:00)
TIME1970 = 2208988800L # 1970-01-01 00:00:00
# connect to server
client = socket.socket(AF_INET, SOCK_DGRAM)
client.sendto(msg, address)
msg, address = client.recvfrom(buf)
t = struct.unpack("!12I", data)[10]
t -= TIME1970
print "\tTime=%s" % time.ctime(t)
Warum verwenden Sie nicht ntplib? http://pypi.python.org/pypi/ntplib/ –