The second chapter mainly introduces the following contents on the basis of the previous chapter:
1. Forkingmixin
2. ThreadingMixIn
3. Select.select
4. Select.epoll
5. Diesel Library
Forkingmixin and ThreadingMixIn belong to the Socketserver (Python2 is the Socketserver) module, which simplifies the task of writing Web servers. It contains four basic server class:
TCPServer uses the TCP protocol to establish continuous, continuous and secure service between the server and the client.
Udpserver uses the UDP protocol, using the method of packet to pass data between the server and the client, there is the possibility of losing the packet, but the transmission speed is very fast;
Unixstreamserver and Unixdatagramserver are less used, similar to TCPServer and Udpserver respectively, but are not available on other platforms based on UNIX-defined sockets.
Figure 1. The inheritance relationship of servers
the above four kinds are synchronous , that is, before the next request begins, the previous request must be completed. Obviously, they do not apply when the request requires a longer processing time. Creating a separate process or thread for each request solves this problem by implementing asynchronous communication between the server and the client, adding Forkingmixin and threadingmixin two classes to this socketserver module.
To create a service, first get a handle class by inheriting the Baserequesthandler class and overriding its handler () method, which will be used to handle requests to the server;
Class Forkingserverrequesthandler (Socketserver.baserequesthandler):
Then instantiate the handle from the server address and the previous step to inherit from Forkingmixin/threadingmixin and tcpserver/udpserver/unixstreamserver/ Unixdatagramserver's server,
Class Forkingserver (Socketserver.forkingmixin, Socketserver.tcpserver,): passserver = Forkingserver ((SERVER_ HOST, Server_port), Forkingserverrequesthandler)
Note: Forkingmixin must be written in front of TCPServer because it overloads the methods of the TCPServer class.
Finally, call the Handler_request () or Server_forever () method to begin processing the request.
Server_thread = Threading. Thread (Target=server.serve_forever)
It is important to note that if the server class inherits from ThreadingMixIn, you need to explicitly specify the processing method when an exception is stopped, and the ThreadingMixIn class defines the Daemon_threads method, which specifies whether the server needs to wait until all threads terminate , the default is False.
Server_thread.setdaemon (True)
The following is a simple example:
Import OS import Socket import threading Import socketserver server_host = ' localhost ' server_port = 0 # tells T He kernel to pick up a port dynamically buf_size = 1024x768 echo_msg = ' Hello ECHO server! ' Class Forkedclient (): "" "A client to test forking server" "" Def __init__ (self, IP, port): # Create A socket Self.sock = Socket.socket (socket.af_inet, socket. SOCK_STREAM) # Connect to the server Self.sock.connect ((IP, Port)) def run (self): "" " Client playing with the server "" "# Send the data to server current_process_id = Os.getpid () print ' PID%s sending echo message to the server: '%s ' '% (current_process_id, echo_msg) Sent_data_length = Self.sock.send (echo_msg) print "Sent:%d characters, so far ..."%sent_data_length # Display Server re Sponse response = SELF.SOCK.RECV (buf_size) print "PID%s Received:%s"% (Current_procESS_ID, Response[5:]) def shutdown (self): "" "Cleanup the Client Socket" "" Self.sock.close () Class Forkingserverrequesthandler (Socketserver.baserequesthandler): def handle (self): # Send the echo back To the client data = SELF.REQUEST.RECV (buf_size) current_process_id = os.getpid () response = '%s:%s '% (current_process_id, data) print "Server sending response [current_process_id:data] = [%s]"%resp Onse self.request.send (response) Return class Forkingserver (Socketserver.forkingmixin, so Cketserver.tcpserver,): "" "Nothing to add here, inherited everything necessary from parents "" "Pass Def Main (): # Launch the server = Forkingserver ((server_host, server_port), forkings Erverrequesthandler) IP, port = server.server_address # Retrieve The port number Server_thread = Threading. Thread (target=server.serve_Forever) Server_thread.setdaemon (True) # don ' t hang on exit Server_thread.start () print ' Server loop run Ning PID:%s '%os.getpid () # Launch the client (s) client1 = forkedclient (IP, Port) client1.run () CLIENT2 = forkedclient (IP, Port) client2.run () # Clean them up server.shutdown () Client1.shutdown () Client2.shutdown () server.socket.close () if __name__ = = ' __main__ ': Main ()
"Python Network Programming Cookbook" Reading notes 2---multiplexing socket I/O for better performance