epoll event notification to receive data, efficiency is higher than the polling
Code:
. 1 Import Socket 2 Import Re . 3 Import SELECT . 4 . 5 . 6 DEF client_server (new_client, recv_data): . 7 # receiving a client request . 8 # recv_data = new_client.recv (1024) .decode ( "UTF-. 8") . 9 # read out branches data 10 recv_data_lines = recv_data.splitlines () . 11 # page regular later matching request is GET 12 # [^ /] denotes a non "/" character + represents at least one or more, [^] long is not a space, * represents zero or more 13 is RET = re.match (R & lt " [^ /] + (/ [^] *) " , recv_data_lines [0]) 14 15 # RET = the re.search (R & lt "/ [^] *", recv_data_lines [0]) # Find the first "/" behind non-whitespace character string 16 file_name ret.group = (. 1 ) . 17 # Open File 18 is the try : . 19 F = Open ( " ./html " + file_name, " RB " ) 20 is the except : 21 is # Create a data return the HEADER 22 is Response = " the HTTP / 1.1 404 the NOT FOUND \ R & lt \ n- " 23 is Response + = " \ R & lt \ n- " 24 Response = + "The NOT FOUND ---- ----- " 25 # send data to the client 26 is new_client.send (response.encode ( " UTF-. 8 " )) 27 the else : 28 # read data 29 html_content = reached, f.read ( ) 30 # the data read out into the body inside bODY 31 is response_body = html_content 32 33 is # Create a data return the HEADER 34 is response_header = " the HTTP / 1.1 OK 200 is \ R & lt \ n- " 35 # tell the browser sends the last length of 36 response_header + = "The Length-the Content:% D \ R & lt \ n- " % len (response_body) 37 [ response_header + = " \ R & lt \ n- " 38 is # the HEADER and spliced together BODY 39 Response = response_header.encode ( " UTF-. 8 " ) + response_body 40 # send data to the client 41 is new_client.send (Response) 42 is 43 is # Close the socket long links do not close off this socket peer client 44 is # new_client.close () 45 46 is 47 DEF main (): 48 # create a socket 49 = tcp_server_socket socket.socket (socket.AF_INET, socket.SOCK_STREAM) 50 # Set the server when the first call to close () that is four times after recovery server resources can be released immediately, thus ensuring the next time you run the program, you can use immediately 51 # SO_REUSEADDR reuse address 52 is tcp_server_socket.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR,. 1 ) 53 is # bound port 54 is tcp_server_socket.bind (( "" , 7890 )) 55 # set status monitor 56 is tcp_server_socket.listen (128 ) 57 is # providing the non-clogging 58 tcp_server_socket.setblocking (False) 59 60 # Create a target epoll 61 is EPL = select.epoll () 62 is 63 is # a listening socket fd corresponding to the registered epoll 64 # whether the input data listener select.EPOLLIN 65 epl.register (tcp_server_socket.filno (), select.EPOLLIN) 66 67 # New socket dictionary storage 68 fd_event_dic = dict () 69 70 # socket client services for the new 71 the while True: 72 # poll () will block until the default os monitoring data to tell the program will come by way of event notification de-clog the return value returned lists 73 is fd_event_list = epl.poll () 74 75 #[(fd, Event), (corresponding to a socket file descriptor, the event descriptor, for example, what can receive calls recv (), etc.)] 76 for fd, Event in fd_event_list: 77 # determines if a listening socket fd sockets of fd 78 IF fd == tcp_server_socket.fileno (): 79 # Create client socket, the receiving client address 80 new_client, client_addr = tcp_server_socket.accept () 81 # client socket fd to register epoll in 82 epl.register (new_client.fileno (), select.EPOLLIN) 83 # the dictionary is stored in the client socket 84 fd_event_dic [new_client.fileno ()] = new_client 85 #If not, the listening socket fd or event notification 86 elif Event == select.EPOLLIN: 87 recv_data = fd_event_dic [fd] .recv (1024) .decode ( " UTF-8 " ) 88 # If there is data 89 IF recv_data : 90 # call client_server 91 is client_server (fd_event_dic [FD], recv_data) 92 # If empty data 93 the else : 94 # calling client socket Close () 95 fd_event_dic [FD] .close () 96 # log off the epoll the client socket fd 97 epl.unregister (FD) 98 # out of the dictionary 99 del fd_event_dic [FD] 100 101 102 # close the socket 103 tcp_server_socket.close () 104 105 106 IF the __name__ == ' __main__ ' : 107 main ()