ツイストを使用して、複数の接続を受け入れる単純なサーバーを作成し、接続されているクライアントの数を数えたいと考えています。このカウントは、clientConnectionMade() を使用して工場で (論理的に) 行っていますが、カウンターの値を更新しません。実際、どこが私の間違いなのかわかりません。少し助けていただければ幸いです。
私のサーバーコード: ( http://bpaste.net/show/26789/にもあります)
import socket
import datetime
from twisted.internet import reactor, protocol
from twisted.internet.protocol import Factory, Protocol
class Echo(protocol.Protocol):
def connectionMade(self):
print "New client connected"
def dataReceived(self, data):
print "Msg from the client received"
if data == "datetime":
now = datetime.datetime.now()
self.transport.write("Date and time:")
self.transport.write(str(now))
elif data == "clientes":
self.transport.write("Numbers of clients served: %d " % (self.factory.numClients))
else:
self.transport.write("msg received without actions")
class EchoFactory(Factory):
protocol = Echo
def __init__(self):
self.numClients = 0
def clientConnectionMade(self):
self.numClients = self.numClients+1
def main():
factory = EchoFactory()
factory.protocol = Echo
reactor.listenTCP(9000,factory)
reactor.run()
# this only runs if the module was *not* imported
if __name__ == '__main__':
main()
エラーは表示されず、カウンター「numClients」が更新されず、理由がわかりません。
ありがとう