Service side
#udp服务端多进程
Import Socketserver
Class My_Server (Socketserver. Baserequesthandler):
def handle (self):
#while True:
Data =self.request[0]
Print (Data.decode (' Utf-8 '))
Print (self.client_address,self.request[1])
#mag =input (' >>>: '). Strip ()
#self. Request[1].sendto (Mag.encode (' Utf-8 '), self.client_address)
# Print (self.request)
# Print (self.client_address)
Self.request[1].sendto (' BBB ' encode (' Utf-8 '), self.client_address)
if __name__ = = ' __main__ ':
Ip_port = (' 127.0.0.1 ', 8083)
Obj =socketserver. Threadingudpserver (ip_port,my_server) #买手机, binding mobile card
Obj.serve_forever () #提供了一个循环
Client
Import socket
Updclient =socket.socket (Socket.af_inet,socket. SOCK_DGRAM) #买手机
Server_ip_port = (' 127.0.0.1 ', 8083) #找到服务端软件
While True: #通信循环
Msg =input (' >>>: '). Strip ()
Updclient.sendto (Msg.encode (' Utf-8 '), Server_ip_port) #发送信息, amount of information, service-side address
Data,server_ip =updclient.recvfrom (1024x768) #接收信息
Print (Data.decode (' Utf-8 '))
Socketserver based on UPD, multithreading of UDP