02select listening server

Time:2021-10-22
# can_ read, can_ write, _ =  Select. Select (inputs, outputs, none, none) ## the first parameter is that we need to listen for a readable socket, the second parameter is that we need to listen for a writable socket, the third parameter is that we need to listen for an abnormal socket, and the fourth parameter is the time limit setting. ## if the monitored socket meets the readable and writable conditions, the returned can, read or can_ Write will have values, and then we can use these return values for subsequent operations. Compared with the select model of UNIX, the return value of the select function is an integer to judge whether the execution is successful. ## the first parameter is the socket on the server side, the second is the socket on the client side stored during operation, and the third stores error information# The focus is on the return value. The first one returns a readable list, the second stores a writable list, and the third stores a # list of error information. Import select, socket, queuefrom time import sleep # create a TCP / IP connection server = socket.socket (socket. Af_inet, socket. Sock_stream) # set the socket to asynchronous mode. Server. Setblocking (false) # bind IP address and port # create an IP, port and local ipserver_ Address = ('localhost ', 8090) print ('starting up on% s port% s'% server_address) # bind IP and port server.bind (server_address) # set the number of server listening server.listen (3) inputs = [server] # process data to be sent outputs = [] # output message queue_ Queues = {} while inputs: # wait for at least one connection to start calling the thread print ("waiting for the next event") # start selecting and listening for input_ The server on the server side of the list starts to listen # once the send and recv functions of the socket are called, this module will be called again # here three parameters are monitored, the first one returns a readable list, the second one stores a writable list, and the third one stores a readable, writable, exceptional = select.select (inputs, outputs, inputs) of error information #Process the input # loop to judge whether a client is connected. When a client is connected, select will trigger for s in readable: # judge whether the server object is currently triggered. When the server object is triggered, If s is server: # establish a connection with the client connection, client_ Address = s.accept() # print the link of the client print ('connection from ', client_address) # set the socket to asynchronous mode. Connection. Setblocking (0) # adds the client object to the listening list. Select will also be triggered when the client sends a message. Inputs. Append (connection) # establishes a separate message queue for the connected client to save the messages sent by the client_ Queues [connection] = queue. Queue () else: # an old user sends a message and processes the reception # because the server receives the client's request when the client connects, the client is also added to the listening list# The message sent by the client will trigger# Judge whether the trigger object is the client # receiving the client's information data = s.recv (1024) # the client is not disconnected if data! = '':# An available client sent data print ('received '% s' from% s'% (data, s.getpeername()) # and put the received message into the message queue of the corresponding client_ Queues [S]. Put (data) # put the socket that needs to reply into the output list and let select listen # here. If we receive a message, we will add the server into the output list# Then select will listen. If s not in outputs: outputs. Append (s) else: # the client is monitored for changes. Either there is a message from the client or the client is disconnected# The client disconnects and removes the listener of the client from the input list. Print ('closing ', client_address) if s in outputs: outputs. Remove (s) inputs. Remove (s) s.close() # removes the message queue del message of the corresponding socket client object_ Queues [S] # processes the output# If there is no client request and no client sends a message, start processing the sent message list# Determine whether a message needs to be sent# Stores which client sent the message. For s in writable: try: # if there is a message in the message queue, get the message to be sent from the message queue. message_ queue = message_ queues.get(s)            sent_ data = ''            if message_ Queues is not none: # if the queue is empty, do not wait (block) send_ data = message_ queue.get_ Nowait () else: # the client has disconnected print ('has closed ') # if the queue is empty, it means that the sent message is finished and needs to be deleted from the queue. except queue.Empty:            print('%s' % (s.getpeername()))            outputs.remove(s)        else:            # print "sending %s to %s " % (send_data, s.getpeername)            # print "send something"            if message_ queue is not None:                s.send(send_data)            else:                print("has closed ")            # del message_ Queues [S] # writable. Remove (s) # print "client% s disconnected"% (client_address) ## handle "exceptional conditions" # handle exceptions # if there are exceptions, delete them from the output queue and close the connection. for s in exceptional:            print('exception condition on', s.getpeername())            # Stop listening for input on the connection            inputs.remove(s)            if s in outputs:                outputs.remove(s)            s.close()            # Remove message queue            del message_ queues[s]        sleep(1)