Server: Import socket Sever = socket.socket () # first establish a tunnel sever.bind (( " 127.0.0.1 " , 9888)) # establish an IP address plus port number sever.listen (5) # set the maximum user five the while True: ATT, IDD = sever.accept () # receiving messages from users the while True: the try : RES = att.recv (1024) # determines whether the user sends a message greater than 1024 Print (res.decode ()) # decode to decode the binary conversion to a normal character. 8-pycharm default UTF the while True: Choice = INPUT (" Enter " .) .Encode () Strip () # see the user information and then himself in the input IF not Choice: the Continue # judge whether the input is empty space-time then continue entering att.send (Choice) # input information to the the client break # end of the except ConnectionResetError aS E: # because it is the end user will report an error when the server is working 24 hours and then the server kick kick like a break
Client Import socket Client = socket.socket () # establish a tunnel the client.connect (( " 127.0.0.1 " , 9888)) # ip address and port number of the server links the while True: Choice = the INPUT ( " Enter " ) .encode () .strip () # linked directly send messages iF Not Choice: Continue client.send (Choice) # contents of the input to the server transmits to the RAS = client.recv (1024) # feedback information is greater than the server determines to accept 1024 Print (ras.decode ()) # print decoded