我有一个使用twisted用python编写的服务器应用程序,我想知道如何杀死我的协议实例(bottalk)。每次我获得一个新的客户端连接时,我都会在内存中看到该实例(打印 Factory.clients).. 但是假设我想从服务器端杀死其中一个实例(删除特定的客户端连接)?这可能吗?我尝试使用 lineReceived 查找短语,如果匹配,则 self.transport.loseConnection()。但这似乎不再引用该实例或其他东西..
class bottalk(LineReceiver):
from os import linesep as delimiter
def connectionMade(self):
Factory.clients.append(self)
print Factory.clients
def lineReceived(self, line):
for bots in Factory.clients[1:]:
bots.message(line)
if line == "killme":
self.transport.loseConnection()
def message(self, message):
self.transport.write(message + '\n')
class botfactory(Factory):
def buildProtocol(self, addr):
return bottalk()
Factory.clients = []
stdio.StandardIO(bottalk())
reactor.listenTCP(8123, botfactory())
reactor.run()