简略版
#服务端from socket import *from time import strftimeip_port = ('127.0.0.1', 9000)bufsize = 1024udp_server = socket(AF_INET, SOCK_DGRAM)udp_server.bind(ip_port)while True: msg, addr = udp_server.recvfrom(bufsize) print("===>", msg) if not msg: time_fmt = '%Y-%m-%d %X' #udp可以传输空,如果没有就用默认格式 else: time_fmt = msg.decode('utf-8') back_msg = strftime(time_fmt) udp_server.sendto(back_msg.encode('utf-8'), addr)udp_server.close()#客户端from socket import *ip_port = ('127.0.0.1',9000)bufsize = 1024udp_client = socket(AF_INET,SOCK_DGRAM)while True: msg = input("请输入时间的格式:").strip() udp_client.sendto(msg.encode('utf-8'),ip_port) data,addr = udp_client.recvfrom(bufsize) print(data.decode('utf-8'))udp_client.close()
真正的时间服务器实在客户端发送指令后,服务端获取时间信息,然后传到客户端,中间关键部分时间格式转换,之后补充出来