1. 程式人生 > >Python socketserver模塊解析

Python socketserver模塊解析

協議 del __init__ thread for python pass soft 技術

參考:https://blog.csdn.net/qq_33733970/article/details/79153938

1.功能簡介

  • socketserver模塊是對socket模塊的再封裝,用於簡化網絡服務器的編寫,並且實現了多用戶的在線,即多並發

2.功能分類

#處理鏈路問題server類
        +------------+
        | BaseServer |
        +------------+
              |
              v
        +-----------+        +------------------+
        | TCPServer |------->| UnixStreamServer |
        +-----------+        +------------------+
              |
              v
        
+-----------+ +--------------------+ | UDPServer |------->| UnixDatagramServer | +-----------+ +--------------------+   
#處理多線程並發:
  "ThreadingMixIn","ThreadingUDPServer","ThreadingTCPServer"
  #處理多進程並發:
  "ForkingUDPServer","ForkingTCPServer", "ForkingMixIn"
#處理通信問題request類
  "BaseRequestHandler", "StreamRequestHandler","DatagramRequestHandler"

3.類繼承關系

●處理多線程並發相關類繼承關系

技術分享圖片 技術分享圖片 threading-inherit-relation

●處理多進程並發相關類繼承關系

技術分享圖片 技術分享圖片 forking-inherit-relation

●處理通信request相關類繼承關系

技術分享圖片 技術分享圖片 request-inherit-relation

4.如何創建socketserver服務

①創建一個請求處理類(FtpServer),繼承BaseRequestHandler類,並重寫父類handle()方法;
②實例化服務器類(TCPServer、UDPServer、UnixStreamServer或UnixDatagramServer),傳入(IP, 端口)及定制的請求處理類(FtpServer);
③server.handle_requese() #只處理一個請求
④server.server_forever() #處理多個請求
⑤server.server_close()   #關閉socket

5.基於TCP協議實現的FTPServer服務調用過程解析

import socketserver

class FtpServer(socketserver.BaseRequestHandler):
    def handle(self):
        pass
ftpserver = socketserver.ThreadingTCPServer((127.0.0.1, 8080), FtpServer) ftpserver.serve_forever()
1.屬性解析順序
ThreadingTCPServer-->ThreadingMixIn-->TCPServer-->BaseServer
2.調用TCPSever類的__init__方法獲取實例化對象
class TCPServer(BaseServer):
    def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True):
        self.server_bind()
        self.server_activate()
3.調用BaseServer類的server_forever方法,並處理請求
class BaseServer:
    def serve_forever(self, poll_interval=0.5):
        self._handle_request_noblock()

    def _handle_request_noblock(self):
        """相當於TCPServer中的self.socket.accept()"""
        request, client_address = self.get_request()
        """接收請求,並處理來次客戶端的請求"""
        self.process_request(request, client_address)
4.調用ThreadingMixIn類的process_request方法,執行process_request_thread方法
class ThreadingMixIn:
    def process_request_thread(self, request, client_address):
        self.finish_request(request, client_address)

    def process_request(self, request, client_address):
        """開啟新線程處理請求"""
        t = threading.Thread(target = self.process_request_thread, args = (request, client_address))
        t.daemon = self.daemon_threads
        t.start()
5.調用BaseServer類的finish_request方法,完成請求,至此鏈接循環的處理完成,開始通訊循環
def finish_request(self, request, client_address):
    """通過實例化定制類(FtpServer)去結束一個請求"""
    self.RequestHandlerClass(request, client_address, self)
6.調用BaseRequestHandler類的__init__方法
class BaseRequestHandler:
    def __init__(self, request, client_address, server):
        """
     
調用了對象自己的handle()方法
     服務端跟客戶端所有的交互都在重寫的handle()方法中
     """ self.handle()

Python socketserver模塊解析