portsock listen( mainsocks append(portsockreadsocks append(portsockmyport + bind it to server port number listenallow pending connects add to main list to identify add to select inputs list bind on consecutive ports event looplisten and multiplex until server process killed print('select-server loop starting'while true#print(readsocksreadableswriteablesexceptions select(readsockswritesocks[]for sockobj in readablesif sockobj in mainsocksfor ready input sockets port socketaccept new client newsockaddress sockobj accept(accept should not block print('connect:'addressid(newsock)newsock is new socket readsocks append(newsockadd to select listwait elseclient socketread next line data sockobj recv( recv should not block print('\tgot'data'on'id(sockobj)if not dataif closed by the clients sockobj close(close here and remv from readsocks remove(sockobjdel list else reselected elsethis may blockshould really select for writes too reply 'echo=>% at % (datanow()sockobj send(reply encode()the bulk of this script is its while event loop at the end that calls select to find out which sockets are ready for processingthese include both main port sockets on which clients can connect and open client connections it then loops over all such ready socketsaccepting connections on main port sockets and reading and echoing input on any client sockets ready for input both the accept and recv calls in this code are guaranteed to not block the server process after select returnsas resultthis server can quickly get back to the top of the loop to process newly arrived client requests and already connected clientsinputs the net effect is that all new requests and clients are serviced in pseudoparallel fashion to make this process workthe server appends the connected socket for each client to the readables list passed to selectand simply waits for the socket to show up in the selected inputs list for illustration purposesthis server also listens for new clients on more than one port--on ports and in our examples because these main port sockets are also interrogated with selectconnection requests on either port can be accepted without blocking either already connected clients or new connection requests appearing on the other port the select call returns whatever sockets in readables are ready for processing--both main port sockets and sockets connected to clients currently being processed network scripting