小编How*_*ugh的帖子

python中的ntp客户端

我在python中编写了一个ntp客户端来查询时间服务器并显示时间和程序执行但是没有给我任何结果.我正在使用python的2.7.3集成开发环境,我的操作系统是Windows 7.这是代码:

# 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" …
Run Code Online (Sandbox Code Playgroud)

python

14
推荐指数
3
解决办法
2万
查看次数

标签 统计

python ×1