Python超级初始化自定义套接字类

时间:2020-08-12 13:57:56

标签: python sockets super

我想写一个继承socket

功能的类

所以我的代码如下:

class walkie_talkie(socket.socket(socket.AF_INET, socket.SOCK_STREAM)):
    def __init__(self):
        self.BUFFERSIZE = 8192
        self.STATE = None
        self.IP = None                              # Server IP
        self.PORT = 5000
        self.NUMBER_OF_UNACCEPTED_CONNECTIONS = 1
        self.bytestream = None
        super().__init__()

我将此类用作:

from walkie_talkie import walkie_talkie

rpi1 = walkie_talkie()

所以我认为使用super().__init__()继承了socket.socket(socket.AF_INET, socket.SOCK_STREAM)的功能

但是,我收到此错误:

File "...\lib\socket.py", line 151, in __init__
    _socket.socket.__init__(self, family, type, proto, fileno)
TypeError: an integer is required (got type str)

有人可以告诉我如何使用超级功能吗? 预先感谢!

编辑:解决方案

我纠正了我的错误:

class walkie_talkie(socket.socket):
    def __init__(self):
        self.BUFFERSIZE = 8192
        self.STATE = None
        self.IP = None                              # Server IP
        self.PORT = 5000
        self.NUMBER_OF_UNACCEPTED_CONNECTIONS = 1
        self.bytestream = None
        super().__init__(socket.AF_INET, socket.SOCK_STREAM)

0 个答案:

没有答案