Good day:
I've been having fun with Twisted. I have my application running fine, with
multiple server and client connections using Telnet. :-)
However, users always want something. I need to send some unique
configuration information to each connection. The connections are made
using connectTCP. My first attempt assumed that the connection was made
when the call to connectTCP was executed. It didn't work! :-(
-------------------------------
from twisted.internet.protocol import ClientFactory
from twisted.internet import reactor
from twisted.conch.telnet import StatefulTelnetProtocol
class testClient(StatefulTelnetProtocol):
def connectionMade(self):
self.title = self.factory.connectString
print "Client Connected: " + self.title
self.setRawMode()
self.factory.connections.append(self)
def connectionLost(self, reason):
if self in self.factory.connections:
self.factory.connections.remove(self)
def rawDataReceived(self,data):
print data + "\n"
class ClusterClientFactory(ClientFactory):
protocol = testClient
def __init__(self):
self.connections = []
self.connectString = ''
def startFactory(self):
print "startFactory: " + self.connectString
def startedConnecting(self, connector):
print "Started connecting: " + str(connector)
print self.connectString
def buildProtocol(self, addr):
print "bulldProtocol: " + str(addr)
print "buildProtocol: " + self.connectString
p = self.protocol()
p.factory = self
return p
if __name__ == '__main__':
def startUp():
factory = ClusterClientFactory()
factory.maxDelay = 120 # two minutes
factory.connectString = "FirstString..."
reactor.connectTCP("localhost", 7300, factory)
factory.connectString = "SecondString"
reactor.connectTCP("localhost", 7300, factory)
reactor.callWhenRunning(startUp)
reactor.run()
----------------------------------
The results are that buildProtocol gets the second string both times.
startedConnecting gets the correct string, but all I have there is a
connection object. I need the correct data in buildProtocol to do it this
way.
------------------------------------
C:\Users\Mark\src\play>python testclient.py
startFactory: FirstString...
Started connecting: