Python socketserver模块解析

时间:2022-05-05 03:31:42

参考: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.类继承关系

●处理多线程并发相关类继承关系

Python socketserver模块解析Python socketserver模块解析
Python socketserver模块解析
threading-inherit-relation

●处理多进程并发相关类继承关系

Python socketserver模块解析Python socketserver模块解析
Python socketserver模块解析
forking-inherit-relation

●处理通信request相关类继承关系

Python socketserver模块解析Python socketserver模块解析
Python socketserver模块解析
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()