1 import socket
2 import time
3
4 if __name__ == '__main__':
5 mysocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
6 host_port = ("127.0.0.1", 9527)
7 mysocket.bind(host_port)
8 #input("wait for msg: ")
9 while True:
10 print('waitting for request')
11 data, addr = mysocket.recvfrom(1024)
12 print("request type: {}, from: {}".format(data.decode(), addr))
13 response = ''
14 if data.decode() == 'time':
15 response = time.strftime("%H:%M:%S", time.localtime())
16 elif data.decode() == 'date':
17 response = time.strftime("%Y-%m-%d", time.localtime())
18 elif data.decode() == 'datetime':
19 response = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
20 elif data.decode() == 'quit':
21 response = "server quit"
22 else:
23 response = "invalid request"
24 mysocket.sendto(response.encode(), addr)
25 if data.decode() == 'quit':
26 exit(0)
1 import socket
2
3 def myinput():
4 while True:
5 myinput = input("input time or date: ")
6 if not myinput:
7 print("empty input")
8 continue
9 if myinput == 'time':
10 return 'time'
11 if myinput == 'date':
12 return 'date'
13 if myinput == 'datetime':
14 return 'datetime'
15 if myinput == 'quit':
16 return 'quit'
17 print('invalid input')
18
19 if __name__=='__main__':
20 mysocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
21 while True:
22 choice = myinput()
23 mysocket.sendto(choice.encode(), ('127.0.0.1', 9527))
24 data, addr = mysocket.recvfrom(1024)
25 print("data: {}, addr: {}".format(data.decode(), addr))
26 if choice == 'quit':
27 print('quit')
28 exit(0)
29