# ### udp protocol server Import socket # socket.SOCK_DGRAM udp protocol on behalf of SK = socket.socket (of the type = socket.SOCK_DGRAM) # bind address sk.bind (( "127.0.0.1", 9000)) # udp as server for the first time must first accept the message msg, cli_addr = sk.recvfrom (1024) Print (msg, cli_addr) msg2 = "Yes, really good, because today Friday" # sendto ( "message to send" .encode (), (ip, port)) sk.sendto (msg2.encode ( "UTF-. 8"), cli_addr) # udp close connection sk.close ()
# ### udp client Import socket SK = socket.socket (of the type = socket.SOCK_DGRAM) msg = "a nice day" # udp send data # sendto ( "message to send" .encode (), (ip, port )) sk.sendto (msg.encode ( "UTF-. 8"), ( "127.0.0.1", 9000)) # receiving UDP data MSG, addr = sk.recvfrom (1024) Print (MSG, addr) RES = MSG .decode ( "UTF-. 8") Print (RES) # udp close connection sk.close ()