Python Learning Notes-socketserver source code anatomy

Source: Internet
Author: User

Examples of previous studies are single-threaded sockets, and if multiple users are connected at the same time, all but the first one will be in a pending state until the current connected client disconnects.

By using Socketserver, we can implement concurrent connections.

The use of Socketserver is simple:


First, let's look at a simple example.


Service side:

Define a class yourself, inherit Socketserver.baserequesthandler;

Then define a method handle ()

The socket and its own class are then specified by Socketserver.threadingtcpserver, and each time the client is connected, an object is automatically instantiated, and the data is continuously cycled through Server_forever ().


#!/usr/bin/env python# -*- coding:utf-8 -*-# author yuan liimport  Socketserverclass mysocketserver (Socketserver. Baserequesthandler):     def handle (self):         conn = self.request        conn.sendall (Bytes ("Welcome  to the test system. ",  encoding= ' Utf-8 '))          while True:            try:                 data = conn.recv (1024x768)                 if  Len (data)  == 0: break                 print ("[%s] sends %s"  %  (Self.clienT_address, data.decode ()))                  conn.sendall (Data.upper ())              except Exception:                 breakif __name__ ==  ' __main__ ':    server =  Socketserver. Threadingtcpserver ((' 127.0.0.1 ',  8009),  mysocketserver)     server.serve_forever ( )


Client:

#!/usr/bin/env python#-*-coding:utf-8-*-# Author Yuan liimport socketip_port = (' 127.0.0.1 ', 8009) s = Socket.socket () s. Connect (ip_port) data = S.RECV (1024x768) print (Data.decode ()) While true:send_data = input ("data>>>") S.send (byte S (send_data, encoding= ' Utf-8 ')) Recv_data = S.recv (1024x768) print (Recv_data.decode ())


The effect above is that multiple clients can connect to the server at the same time, enter letters, and return uppercase letters.


The client has nothing to say, this is the same as a single-threaded operation, but the server is a mess to see. We can figure out the process of his execution by dissecting the source code.


The basic structure of this class is shown below, and we run it in order to see how he calls it.


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/89/3C/wKiom1gNmjTj7TH2AAK6-Px_JOg280.png "title=" Capture.png "alt=" Wkiom1gnmjtj7th2aak6-px_jog280.png "/>


1. The first execution of this sentence, it is obvious that Thredingtcpserver is a class, dot go in to see his instantiation process

Server = Socketserver. Threadingtcpserver ((' 127.0.0.1 ', 8009), Mysocketserver)

2. Point the CTRL key, click this class, Pycharm will automatically open the corresponding source code, you can see this class has inherited two parent class Thredingmixin and TCPServer

Class Threadingtcpserver (ThreadingMixIn, tcpserver): Pass

Because his content is pass, nothing to do, according to the Order of inheritance, we continue to go up (from left to right) to find the Init constructor;


3. ThreadingMixIn there is no constructor, then continue to the right, tcpserver inside there is a constructor, but he called his parent class Baseserver constructor, along the look, found that he is encapsulated a few values inside, pay attention to self. Requesthandlerclass = Requesthandlerclass The class of our own definition is passed in.

def __init__ (self, server_address, Requesthandlerclass): "" "Constructor.        May is extended, do not override. "" " Self.server_address = Server_address self. Requesthandlerclass = Requesthandlerclass Self.__is_shut_down = Threading. Event () Self.__shutdown_request = False


4. Next, in TCPServer's constructor, he performs the Bind,listen operation, which is the same as the single-threaded operation. At this end, an initialization process is almost complete.


5. Next, perform the operation of Server.serve_forever (), and let's see how the interior is called. In this function, selector's IO multiplexing technique is used to iterate through the operation of a file. The _handle_request_noblock () function is then called

  try:            # XXX:  consider using another file descriptor or connecting to the             # socket to wake this  up instead of polling. Polling reduces our             # responsiveness to a shutdown request  and wastes cpu at all other             # times.             With _serverselector ()  as selector:                 selector.register (self, selectors. Event_read)                 while not self.__shutdown_request:                     ready =  selector.select (Poll_interval)                      if ready:                         self._handle_ Request_noblock ()                      self.service_actions ()


6. Each time the function is called to remember the order of the search, from the bottom up, from left to right, and finally in the top of the baseserver again find this function, the function inside called the Process_request function

"" "try:request, client_address = Self.get_request () except Oserror:return if Self.verify_request (Request, client_address): try:self.process_request (Request, client_address )

Be sure to remember the order of inheritance!! Order!! Order!!


Because Baseserver own Process_request method, Threadingtcpserver also has the same name method, when he calls, in order, is to execute the Threadingtcpserver inside the method!!


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/89/3A/wKioL1gNoxaThqRFAAB5Z_o7GWs657.png "title=" 2.PNG " alt= "Wkiol1gnoxathqrfaab5z_o7gws657.png"/>


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/89/3D/wKiom1gNouDj48lSAABEODjRuN0521.png "title=" 1.PNG " alt= "Wkiom1gnoudj48lsaabeodjrun0521.png"/>


Can see him open a multi-threaded

def process_request (self, request, client_address): "" "Start a new thread to process the request." " t = Threading.  Thread (target = self.process_request_thread, args = (request, client_address)) T.daemon = Self.daemon_threads T.start ()

Inside the process_request_thread he called, he called Finsih_request.

    def process_request_thread (self,  request, client_address):         "" "Same as in  baseserver but as a thread.        in  addition, exception handling is done here.          "" "        try:             self.finish_request (request, client_address)              self.shutdown_request (Request)          except:            self.handle_error (Request ,  client_address)             self.shutdown_ Request (Request) 


Finish_request has an instantiated operation on our custom classes.

def finish_request (self, request, client_address): "" "," "", "" "," "," "" "" Self. Requesthandlerclass (Request, client_address, self)


Since our custom class does not have constructors, he will go to the parent class to look for, and the parent will try to execute the handle () method, which is why we need to define a method with the same name in the custom class, and then put all the content that needs to be executed here.

def __init__ (self, request, client_address, server): Self.request = Request Self.client_address = Client_add Ress self.server = Server Self.setup () Try:self.handle ()


Here, Socketserver, a complete process is over.

This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1864956

Python Learning Notes-socketserver source code anatomy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.