基于upd的socketserver,即udp的多线程

服务端

#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)#买手机,绑定手机卡
obj.serve_forever()#提供了一个循环


客户端
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) #发送信息,信息量,服务端地址
data,server_ip =updclient.recvfrom(1024) #接收信息
print(data.decode('utf-8'))

转载于:https://www.cnblogs.com/IQ-Python/p/6814529.html

你可能感兴趣的:(网络)