I was trying to count the number of active protocols in twisted but i got an error:
exceptions.AttributeError: Factory instance has no attribute 'numProtocols'
Below is the code:
class EchoPro(Protocol):
def connectionMade(self):
self.factory.numProtocols = self.factory.numProtocols+1
if self.factory.numProtocols > 100:
self.transport.write("Too many connections, try later")
self.transport.loseConnection()
def connectionLost(self, reason):
self.factory.numProtocols = self.factory.numProtocols-1
def dataReceived(self, data):
self.transport.write(data)