一、Socketserver

   服务端代码:

 

import socketserver

class MServer(socketserver.BaseRequestHandler):
    def handle(self):
        while 1:
            from_c_msg = self.request.recv(1024)
            print('客户端说:' + from_c_msg.decode('utf-8'))
            to_c_msg = input('对客户端说:')
            self.request.send(to_c_msg.encode('utf-8'))


if __name__ == '__main__':
    IP_PORT = ('127.0.0.1', 9000)
    server = socketserver.ThreadingTCPServer(IP_PORT, MServer)
    server.serve_forever()
    print(100, server.server_address)

  

 

客户端:

import socket


client = socket.socket()
client.connect(('127.0.0.1', 9000))
while 1:
    to_s_msg = input('对服务端说:')
    client.send(to_s_msg.encode('utf-8'))
    from_s_msg = client.recv(1024)
    print('服务端说:' + from_s_msg.decode('utf-8'))