Udp实现省略编码
class My_Socket(socket.socket): def __init__(self, encoding='utf-8'): self.encoding = encoding super(My_Socket, self).__init__(type=socket.SOCK_DGRAM) def mysendto(self, msg, addr): return self.sendto(msg.encode(self.encoding), addr) def myrecvfrom(self, num): data, addr = self.recvfrom(num) return data.decode(self.encoding), addr
import my_UDP sk = my_UDP.My_Socket() while 1: msg = input('>>>') sk.mysendto(msg,('127.0.0.1', 8080)) data, addr = sk.myrecvfrom(1024) print(data)
import my_UDP sk = my_UDP.My_Socket() sk.bind(('127.0.0.1', 8080)) while 1: data, addr = sk.myrecvfrom(1024) print(data) msg = input('>>>') sk.mysendto(msg, addr)