Tcp实现省略编码
import socket class My_socket(socket.socket): def __init__(self, encoding='utf-8'): self.encoding = encoding super(My_socket, self).__init__() def mysend(self, data): return super(My_socket, self).send(data.encode(self.encoding)) def my_send(self, data, conn): return conn.send(data.encode(self.encoding)) def myrecv(self, num): msg = super(My_socket, self).recv(num).decode(self.encoding) return msg def my_recv(self, buffersize, conn): msg = conn.recv(buffersize).decode(self.encoding) return msg
import my_TCP sk = my_TCP.My_socket() sk.bind(('127.0.0.1', 7084)) sk.listen(4) while 1: conn, addr = sk.accept() print(conn, type(conn)) while 1: res_s = sk.my_recv(1024, conn) print(res_s) if res_s == 'q': break res_f = input('>>>>>') sk.my_send(res_f, conn) if res_f == 'q': break conn.close() sk.close()
import my_TCP sk = my_TCP.My_socket() adress = ('127.0.0.1', 7084) sk.connect(adress) while 1: meg = input('>>>') sk.mysend(meg) if meg == 'q': break res_s = sk.myrecv(1024) if res_s == 'q': break print(res_s) sk.close()