Service side
ImportSocketserverclassMyserver (socketserver. Baserequesthandler):" "defines a class that must inherit the Baserequesthandler under Socketserver" " defhandle (self):#rewrite its internal handler method #internally encapsulated Self.request, which is equivalent to the conn in the socket whileTrue:recv= SELF.REQUEST.RECV (1024)#accept content in byte form if notrecv: Break Print(Str (recv, encoding="Utf-8")) Self.request.send (recv+bytes ("I'm your dad .", encoding="Utf-8"))#Create a multi-tasking server, each connection I create a thread to interact with itServer = Socketserver. Threadingtcpserver (("localhost", 8080), Myserver) Server.serve_forever ()
Client 1
Import socket
Client = Socket.socket (socket.af_inet, socket. SOCK_STREAM)
Client.connect (("localhost", 8080))
While True:
INP = input ("Please enter what you want to send:")
Client.send (Bytes (INP, encoding= "Utf-8"))
recv = CLIENT.RECV (1024)
Print (str (recv, encoding= "Utf-8"))
Client 2
Import socket
Client = Socket.socket (socket.af_inet, socket. SOCK_STREAM)
Client.connect (("localhost", 8080))
While True:
INP = input ("Please enter what you want to send:")
Client.send (Bytes (INP, encoding= "Utf-8"))
recv = CLIENT.RECV (1024)
Print (str (recv, encoding= "Utf-8"))
Client 3
Import socket
Client = Socket.socket (socket.af_inet, socket. SOCK_STREAM)
Client.connect (("localhost", 8080))
While True:
INP = input ("Please enter what you want to send:")
Client.send (Bytes (INP, encoding= "Utf-8"))
recv = CLIENT.RECV (1024)
Print (str (recv, encoding= "Utf-8"))
Python--socketserver