Python 如何创建具有twisted可搜索属性的连接协议的运行列表?

Python 如何创建具有twisted可搜索属性的连接协议的运行列表?,python,tcp,server,twisted,Python,Tcp,Server,Twisted,我正在尝试创建一个服务器,该服务器接受具有当前连接运行列表的TCP连接。我目前的工厂和协议如下: class KConnectProtocol(Protocol): ... # Adds +1 to the client count whenever a new connection is made, while # also putting a log entry for the client IP and time stamp. def connectio

我正在尝试创建一个服务器,该服务器接受具有当前连接运行列表的TCP连接。我目前的工厂和协议如下:

class KConnectProtocol(Protocol):
...

    # Adds +1 to the client count whenever a new connection is made, while     
    # also putting a log entry for the client IP and time stamp.
    def connectionMade(self):
        self.client = self.transport.client
        log.msg('K connected from: ' + self.client[0])
        self.factory.numProtocols = self.factory.numProtocols + 1
        log.msg('There are ' + str(self.factory.numProtocols) + ' k connected.')

        self.factory.kList.append(self)
...

class KFactory(ServerFactory):

     # numProtocols keeps track of the number of clients connected to the server at
     # any given point. It's an attribute of the factory so it can be called globally
     protocol = KConnectProtocol
     numProtocols = 0
     kList = []
     uid = []
现在,我希望能够根据每个协议的UID搜索kList。Ie

k1.uid = 123
k2.uid = 234
k3.uid = 345

kList = (k1, k2, k3)
现在,列表中的每个项目都代表一个唯一的TCP连接。我想在kList中搜索属性“234”。一旦我确定了对象的索引,我应该能够执行
k2.transport.write(“whoope”)
来通过特定的TCP连接发送东西。然而,我遇到了一个障碍,我不确定在哪里声明该属性,也不确定列表是否可以搜索到那个程度

为了涵盖所有问题,我的问题如下:

class KConnectProtocol(Protocol):
...

    # Adds +1 to the client count whenever a new connection is made, while     
    # also putting a log entry for the client IP and time stamp.
    def connectionMade(self):
        self.client = self.transport.client
        log.msg('K connected from: ' + self.client[0])
        self.factory.numProtocols = self.factory.numProtocols + 1
        log.msg('There are ' + str(self.factory.numProtocols) + ' k connected.')

        self.factory.kList.append(self)
...

class KFactory(ServerFactory):

     # numProtocols keeps track of the number of clients connected to the server at
     # any given point. It's an attribute of the factory so it can be called globally
     protocol = KConnectProtocol
     numProtocols = 0
     kList = []
     uid = []
  • 在哪里创建属性(
    uid
    ),以便每个连接在协议init中都有一个唯一的标识符
  • 如何列出可在该属性上搜索的对象(协议)

  • 我是python新手,也是twisted和networking的新手,任何正确方向的帮助或观点都会很有帮助!(顺便说一句,我搜索了这么多,找到了一些答案,但似乎没有找到答案。)

    我找到了自己的答案,我想把它贴在这里

    为了为每个协议实例创建唯一的名称,我在ConnectionMode方法中创建了属性:

    class KConnectProtocol(Protocol):
    
        # Adds +1 to the client count whenever a new connection is made, while
        # also putting a log entry for the client IP and time stamp.
        def connectionMade(self):
            self.client = self.transport.client
            log.msg('K connected from: ' + self.client[0])
            self.factory.numProtocols = self.factory.numProtocols + 1
            log.msg('There are ' + str(self.factory.numProtocols) + ' k connected.')
    
            self.factory.kList.append(self)
    
            self.kUID = ""
    
    这会将其作为属性添加到创建的每个实例(连接)中,因此您可以为每个实例设置自己的标识符

        for x in self.factory.kList:
            if x.kUID == "12345":
                x.transport.write("It works!\n")
    
    在这里,我能够识别一个特定的连接,并使用该UID向该连接发送信息

    执行“打印x.”实际上会打印该实例的所有内容,这就是我找到答案的原因。通过工厂跟踪kList,这可用于在特定工厂范围内的任何位置引用连接


    我仍在努力提高效率,但希望这也能帮助其他人

    您不应该使用self.transport.client;这是一个意外暴露的属性,仅在某些实现中存在。ITransport定义了
    getPeer
    ——这是您在这种情况下想要的。此外,您应该将其视为一个而不是一个元组,因此请使用
    .host
    属性。否则:答案正确!