Python--socketserve源码分析(一)

时间:2022-05-20 05:12:34

Python--socketserve源码分析(一)

class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass

实现原理:

  s =socketserver.ThreadingTCPServer(参数1,参数2,......)

  根据ThreadingTCPServer的基类顺序,依次匹配各基类的构造函数参数

  若第一个没构造函数则向后匹配

  若有构造函数但参数不匹配则报错

  若有构造函数且参数匹配则调用基类构造函数,并停止匹配过程

情况1:

#coding:UTF-8

class Base:def fun(self):
print ("Base fun") class B:
def __init__(self, a, b):
print (a,b) class A(Base, B):
pass if __name__ == "__main__":
a = A(1,2)

>>>> 1,2

情况2:

#coding:UTF-8

class Base:
def __init__(self):
self.ip = "192.168.3.8"
self.port = 1122
self.fun() def fun(self):
print ("Base fun") class B:
def __init__(self, a, b):
print (a,b) class A(Base, B):
pass if __name__ == "__main__":
a = A(1,2)

>>>> Traceback (most recent call last):

>>>> File "test.py", line 20, in <module>
>>>> a = A(1,2)
>>>> TypeError: __init__() takes 1 positional argument but 3 were given
情况3:

#coding:UTF-8

class Base:
def __init__(self, a, b):
self.ip = "192.168.3.8"
self.port = 1122
self.fun() def fun(self):
print ("Base fun") class B:
def __init__(self, a, b):
print (a,b) class A(Base, B):
pass if __name__ == "__main__":
a = A(1,2)

>>>> Base fun