Using the Select module for IO multiplexing service side
1 ImportSocket2 ImportSelect3 #only Select.select is supported on Windows, poll Epoll is not supported4 5HOST ="127.0.0.1"6PORT = 99997Buffsize = 10248ADDR =(HOST, PORT)9 #creating server-side listener socketsTenTcpsocket =Socket.socket (socket.af_inet, socket. SOCK_STREAM) One Tcpsocket.bind (ADDR) ATcpsocket.listen (5) - tcpsocket.setblocking (0) - the - #listening for read events -Inputs =[Tcpsocket] - #listening to write events +outputs = [] - #Monitoring Exception Events +exceptions = [] A at whileTrue: - #whenever there is a client connection, the returned r_list contains the listener's server socket Tcpsocket -R_list, w_list, e_list = select.select (inputs, outputs, exceptions, 1) - Print(len (inputs)) - forRinchr_list: - ifr = =Tcpsocket: inRequest, addr =r.accept () - request.setblocking (0) to #Add the client socket to the listener list, and the next time the client sends the data, the socket can be included in the R_list . + inputs.append (Request) - #If the socket is not equal to tcpsocket, it means that the client sends the data the Else: * Try: $Databytes = R.RECV (1024)Panax NotoginsengDatastr = str (databytes, encoding="Utf-8") - Print("received from", R.getpeername ()," : ", Datastr) theR.sendall (Bytes ("you have received your e-mail"+ Datastr, encoding="Utf-8")) + #Description Client Disconnects A #Connectionreseterror the exceptException as E: + #remove the client socket from the listener list - Inputs.remove (R) $ r.close () $Tcpsocket.close ()
Python Network programming Note two