Sever side:
ImportSocketserverclassMytcphandler (socketserver. Baserequesthandler):defhandle (self):" "the code inside the try is the function to be implemented, is now implemented in lowercase to uppercase, can be customized, try is at the end of the client without error" " Try: whileTrue:self.data= SELF.REQUEST.RECV (1024). Strip ()Print("{} wrote:". Format (Self.client_address[0]))Print(Self.data) Self.request.sendall (Self.data.upper ())exceptConnectionreseterror as E:Print(e)if __name__=="__main__": HOST, PORT="localhost", 6969Server=Socketserver. Threadingtcpserver (HOST, PORT), Mytcphandler) Server.serve_forever ()View Code
Client side:
ImportSocketsock=Socket.socket ()#Sock.connect ((' 120.78.197.182 ', 51102))Sock.connect (('localhost', 6969)) Flag=False while notFlag:data= Input (">>:"). Strip ()ifdata = ="b": Flag =TrueifLen (data) = = 0:ContinueSock.send (Data.encode ()) Recv_data= Sock.recv (8096) Print("recv:", Recv_data.decode ())#Recv_data = B ' #Recv_len = 0 #cmd_length = Int (SOCK.RECV (8096). Decode ()) #Cli_ack = sock.send (b ' ack ') #print (' Cmd_len ', cmd_length) #While Recv_len! = cmd_length: #data = SOCK.RECV (1024x768) #Recv_data + = Data #Recv_len + = len (data) #print (' recv: ', Recv_data.decode ())Sock.close ()View Code
Python multithreaded Concurrent Socket instance