# Method: sk.setblocking (False) # server: Import Socket SK = socket.socket () sk.setblocking (False) sk.bind (( ' 127.0.0.1 ' , 8080 )) sk.listen () # define the empty for storing a list of individual Conn Conn_List = [] # define removal list for the client to delete terminal disconnects Conn_List Conn Conn_Del = [] the while . 1 : the try : Conn, addr = sk.accept () # If it is blocked IO, at this point the program will always be here waiting for Conn_List.append (conn) # overheard conn conn object into the list, namely multi-client connections exceptBlockingIOError: '' ' If there is no client connected to try, the code block is executed, the client determines whether to transmit data to the server ' '' # traversing conn list to see if there is no data sent by the client for conn in Conn_List: try : # attempt to receive data info = conn.recv (1024) .decode ( ' UTF-. 8 ' ) # Note! If the client properly closed, that is, the client performs a close, the server receives an empty IF not info: # If you receive an empty, prove the client disconnects, add the conn to delete the list for later deleted Conn_List corresponding Conn Conn_Del.append (Conn) Print ( " the client has a disconnected ') # Close the corresponding server in conn conn.Close () the else : '' ' server logic level ' '' # If the client receives the data, the logic returns the data to the client layer Print ( ' which is received from the client } {data: {} ' .format (Conn, info)) # return data to the client (e.g., client lowercase to uppercase data back to the client) conn.send (info.upper () encode (. ' . 8-UTF ' )) the except BlockingIOError: ' '' if the current cycle is not the transmission data conn, the code block is executed, i.e., the next communication conn '' ' Continue the exceptConnectionResetError: '' ' If this cycle forced to withdraw from the conn, no action ' '' Pass # Delete to delete the list of customers Conn_List has been properly disconnected end conn IF Conn_Del: for conn in Conn_Del: Conn_List.remove (conn) # after the finish to remove the normal close links clients should delete the list empty, otherwise it will error Conn_Del = [] # clients: Import socket SK = socket.socket () sk.connect (( ' 127.0.0.1 ' , 8080 )) the while . 1 : msg_s = INPUT ( ' >>> ') if not msg_s:continue if msg_s == 'q':break sk.send(msg_s.encode('utf-8')) print(sk.recv(1024).decode('utf-8')) sk.close()