SocketServer 中很是重要的两个基类就是:BaseServer 和 BaseRequestHandler
在SocketServer 中也提供了对TCP以及UDP的高级封装,此次咱们主要经过分析关于TCP的处理逻辑来对SocketServer模块进行一个很好的理解和学习缓存
TCPServer 继承了BaseServer,初始化的时候,进行了socket套接字的建立。网络
def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True): """Constructor. May be extended, do not override.""" BaseServer.__init__(self, server_address, RequestHandlerClass) self.socket = socket.socket(self.address_family, self.socket_type) if bind_and_activate: try: self.server_bind() self.server_activate() except: self.server_close() raise
在这里咱们看到了咱们很是熟悉的关于socket的建立的内容:
self.socket = socket.socket(self.address_family, self.socket_type)
经过socket模块建立了socket对象,接着调用了server_bind和server_activatesocket
源码内容以下:tcp
def server_bind(self): """Called by constructor to bind the socket. May be overridden. """ if self.allow_reuse_address: self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.socket.bind(self.server_address) self.server_address = self.socket.getsockname()
这里咱们看到了很是熟悉的操做socket.bind方法,以及设置了socket的相关属性ide
源码内容以下:函数
def server_activate(self): """Called by constructor to activate the server. May be overridden. """ self.socket.listen(self.request_queue_size)
一样的这里的调用也很是简单就是执行了socket.listen学习
在TCPServer类中咱们还看到了get_request方法,源码内容以下:this
def get_request(self): """Get the request and client address from the socket. May be overridden. """ return self.socket.accept()
这个的调用其实咱们能够在BaseServer这个基类中看到,咱们以前看过关于BaseServer中这个基类的源码
咱们能够从咱们调用BaseServer基类中的serve_forever方法查看,这里是调用了_handle_request_noblock方法,我继续查看_handle_request_noblock这个源码spa
def _handle_request_noblock(self): """Handle one request, without blocking. I assume that selector.select() has returned that the socket is readable before this function was called, so there should be no risk of blocking in get_request(). """ try: request, client_address = self.get_request() except OSError: return if self.verify_request(request, client_address): try: self.process_request(request, client_address) except: self.handle_error(request, client_address) self.shutdown_request(request) else: self.shutdown_request(request)
能够看到这里最后是调用了process_request来处理请求,继续查看源码:code
def process_request(self, request, client_address): """Call finish_request. Overridden by ForkingMixIn and ThreadingMixIn. """ self.finish_request(request, client_address) self.shutdown_request(request) def finish_request(self, request, client_address): """Finish one request by instantiating RequestHandlerClass.""" self.RequestHandlerClass(request, client_address, self)
从上面的代码咱们能够到最后是在finish_request中实例化了RequestHandlerClass
咱们 这个时候查看一下BaseRequestHandler这个基类的源码以下:
class BaseRequestHandler: """Base class for request handler classes. This class is instantiated for each request to be handled. The constructor sets the instance variables request, client_address and server, and then calls the handle() method. To implement a specific service, all you need to do is to derive a class which defines a handle() method. The handle() method can find the request as self.request, the client address as self.client_address, and the server (in case it needs access to per-server information) as self.server. Since a separate instance is created for each request, the handle() method can define other arbitrary instance variables. """ def __init__(self, request, client_address, server): self.request = request self.client_address = client_address self.server = server self.setup() try: self.handle() finally: self.finish() def setup(self): pass def handle(self): pass def finish(self): pass
在初始化函数里咱们看到了调用setup()方法,这setup在StreamRequestHandler会被重写
TCPServer实现了使用tcp套接字的网络服务,Handler方面则是对应的StreamRequestHandler。它继承了BaseRequestHandler。基类的setup方法和finish方法被它重写,用于经过链接实现缓存文件的读写操做。
源码内容以下:
def setup(self): self.connection = self.request if self.timeout is not None: self.connection.settimeout(self.timeout) if self.disable_nagle_algorithm: self.connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True) self.rfile = self.connection.makefile('rb', self.rbufsize) self.wfile = self.connection.makefile('wb', self.wbufsize)
这里主要设置了对应链接的属性,其中建立了两个对象很是重要:
一个可读(rfile)和一个可写(wfile)的“文件”对象
可是实际并非建立了文件,而是封装了读取数据和发送数据的操做,抽象成了对文件的操做
能够理解:
self.rfile 就是读取客户端数据的对象,它有一些方法能够读取数据。self.wfile则是用来发送数据给客户端的对象。
实现TCP服务须要使用TCPServer和StreamRequestHandler共同协做