Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python套接字.accept()抽象套接字类型_Python_Sockets - Fatal编程技术网

python套接字.accept()抽象套接字类型

python套接字.accept()抽象套接字类型,python,sockets,Python,Sockets,我正在编写一个包含多个套接字的程序,使用select语句使用以下代码管理它们: while not self.stopped: input_ready, output_ready, except_ready = select.select(self.input_sockets, [], [], 5) if (not input_ready) and (not output_ready) and (not except_ready): pri

我正在编写一个包含多个套接字的程序,使用select语句使用以下代码管理它们:

while not self.stopped:
        input_ready, output_ready, except_ready = select.select(self.input_sockets, [], [], 5)
        if (not input_ready) and (not output_ready) and (not except_ready):
            print("Timed Out")
        else:
            for s in input_ready:
                s.process_data()
为此,我创建了一个从
socket.socket
抽象出来的类,并添加了方法
process\u data
。我有一个这样定义的类,它绑定到套接字以侦听入站连接,当调用
process\u data()
时,它使用
(sock,address)=self.accept()
接受连接。。。然后,我将
sock
添加到
input\u sockets
数组中,以便在select中使用,但显然accept方法返回一个socket,而不是带有的抽象类,因此没有
process\u data()
方法,因此这会导致错误

有人能想出一种方法,让我使用
accept()
方法返回我自己的抽象套接字类,而不是普通套接字吗

谢谢

编辑:

目前,我找到了一个很好的解决方案——我没有创建抽象的套接字类,而是创建了一个标准类,如下所示:

class DirectConnection():

  def __init__(self, sock):
    self.socket = sock

  def fileno(self):
    return self.socket.fileno()

  def process_data(self):
    print("Got data")
然后从我的监听插座

(sock, address) = self.accept()
socketmanager.monitor_socket(DirectConnection(sock))
select.select
使用套接字对象的fileno属性,因此通过定义一个
fileno()
方法,返回传递到类中的套接字的
fileno()
,我现在可以让
select.select
在这个类上调用我的方法,然后我可以指示它从传入的套接字发送/接收数据


这要归功于:而且我认为没有任何方法可以完全做到你想要的。您必须使用委托而不是继承,这意味着将套接字对象传递到您自己类的实例中,然后重写
\uu getattr\uuu()
\uu setattr\uuu()
,以有效地将类的大多数方法/属性调用传递到套接字对象上。这涵盖了总体思路::)我刚刚查看了相同的链接。我编辑了我的帖子,提供我的工作。感谢您的帮助,这是我使用python的头几个星期,非常感谢您的帮助。