Based on select models of network IO

# Difference poll and epoll and select the 
    '' ' 
      select and poll have a common mechanism, and are based in rotation the way to ask the kernel, there is no data ready to 
      select a maximum listen event of restrictions, limitations 1024 32-bit machine ,, 6 machine limit 2048 
      poll did not, in theory, can open an infinite poll, 1G memory is probably enough for you to open 10W event to listener 
      epoll is best, using the callback mechanism to solve the select and poll common problems 
      and epoll theoretically unlimited number of listeners can also open the event 
    '' ' 

# IO multiplexing 
    ' '' 
        blocking IO 
        non-blocking IO 
        multiplexing IO 
        asynchronous IO python can not achieve, but there are tornado framework comes naturally asynchronous 
    ' ' 

# server 
Import Socket
 Import SELECT 

SK = socket.socket () 
sk.bind (( ' 127.0.0.1 ' ,8080))
sk.listen () 

Conn_Del = []         # delete the definition list 
RLIST = [SK]          # is used to allow listeners to help select all interfaces 
# select: Windows / Linux is to monitor the event there is no data coming 
# poll: Linux can do select work 
# the epoll: Linux can do a similar job 

the while . 1 : 
    R & lt, W, X = select.select (rlist, [], [])   # will pass rlist select, when there is a reaction which interface in rlist, returns this list to r 
    IF r:
         for I in r:                      # looping through to see if there is a reaction interface or conn sk, sk because if it is, the back will also be added to this list conn 
            IF I == sk:
                 # If is sk, indicates a connection request the client
                conn, addr = i.accept () 
                rlist.append (conn)       # to connect the new client added to rlist, continue to allow select to help monitor 
            the else :
                 # If conn, to request the data on behalf of a client (in this case the customer end connection has been established) 
                # following exception handling model to solve the blocking and non-blocking IO IO model similar to the model 
                the try : 
                    MSG = i.recv (1024) .decode ( ' UTF-. 8 ' )
                     IF  Not MSG:
                         # client normally closed returned to an empty server that the client performs the use Close () 
                        Conn_Del.append (i) 
                        i.close () 
                    the else :
                         #Server-side logic layer (in uppercase returned to the client string) 
                        Print ( " receives the message from the client {} {} " .format (I, MSG)) 
                        i.send (msg.upper (). Encode ( ' UTF-. 8 ' ))
                 the except ConnectionResetError:
                     # client forced off 
                    Pass 

        IF Conn_Del:
             for Conn in Conn_Del: 
                rlist.remove (Conn) 
            Conn_Del.clear () 


sk.close () 




# client 
Import Socket 
SK = socket.socket () 
sk.connect (( ' 127.0.0.1',8080))

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()

 

Guess you like

Origin www.cnblogs.com/god-for-speed/p/11719114.html