Good day:<br><br>I&#39;ve been having fun with Twisted.  I have my application running fine, with multiple server and client connections using Telnet.  :-)<br><br>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&#39;t work!  :-(<br>
<br>-------------------------------<br><br>from twisted.internet.protocol import ClientFactory<br>from twisted.internet import reactor<br>from twisted.conch.telnet import StatefulTelnetProtocol<br><br>class testClient(StatefulTelnetProtocol):<br>
<br>    def connectionMade(self):<br>        self.title = self.factory.connectString<br>        print &quot;Client Connected: &quot; + self.title<br>        self.setRawMode()<br>        self.factory.connections.append(self)<br>
<br>    def connectionLost(self, reason):<br>        if self in self.factory.connections:<br>            self.factory.connections.remove(self)<br><br>    def rawDataReceived(self,data):<br>        print data + &quot;\n&quot;<br>
<br>class ClusterClientFactory(ClientFactory):<br><br>    protocol = testClient<br><br>    def __init__(self):<br>        self.connections = []<br>        self.connectString = &#39;&#39;<br><br>    def startFactory(self):<br>
        print &quot;startFactory: &quot; + self.connectString<br><br>    def startedConnecting(self, connector):<br>        print &quot;Started connecting: &quot; + str(connector)<br>        print self.connectString<br><br>
    def buildProtocol(self, addr):<br>        print &quot;bulldProtocol: &quot; + str(addr)<br>        print &quot;buildProtocol: &quot; + self.connectString<br><br>        p = self.protocol()<br>        p.factory = self<br>
        return p<br><br>if __name__ == &#39;__main__&#39;:<br><br><br>    def startUp():<br><br>        factory = ClusterClientFactory()<br>        factory.maxDelay = 120  #  two minutes<br><br>        factory.connectString = &quot;FirstString...&quot;<br>
        reactor.connectTCP(&quot;localhost&quot;, 7300, factory)<br><br>        factory.connectString = &quot;SecondString&quot;<br>        reactor.connectTCP(&quot;localhost&quot;, 7300, factory)<br><br>    reactor.callWhenRunning(startUp)<br>
    reactor.run()<br><br>----------------------------------<br><br>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.<br>
<br>------------------------------------<br><br>C:\Users\Mark\src\play&gt;python testclient.py<br>startFactory: FirstString...<br>Started connecting: &lt;twisted.internet.tcp.Connector instance at 0x01DAB620&gt;<br>FirstString...<br>
Started connecting: &lt;twisted.internet.tcp.Connector instance at 0x01E0EFD0&gt;<br>SecondString<br>bulldProtocol: IPv4Address(TCP, &#39;127.0.0.1&#39;, 7300)<br>buildProtocol: SecondString<br>Client Connected: SecondString<br>
Welcome to SimpleServer5<br><br><br><br>bulldProtocol: IPv4Address(TCP, &#39;127.0.0.1&#39;, 7300)<br>buildProtocol: SecondString<br>Client Connected: SecondString<br>Welcome to SimpleServer5<br><br>--------------------------<br>
<br><br>My &quot;best&quot; idea is to pass the strings to the factory in a dictionary
indexed by the IP address and the port.  Then, buildProtocol() can use that to
recover the string and I can use reactor.resolve()
to translate the host name to the IP address.  The real application won&#39;t have multiple connections to the same host and port like this example, so this would work.<br>
<br>There MUST be a better way.  :-)<br><br>My &quot;simpleserver&quot; is below if you want to run this.<br><br>THANKS for all of your help.  I have the basic application running now (including a Tkinter GUI  :-) ) and Twisted has saved me hundreds or even thousands of lines of code...<br>
<br>Mark Bailey<br><br>------------------------------<br><br>from twisted.conch.telnet import StatefulTelnetProtocol<br>from twisted.internet import reactor, protocol<br>from twisted.protocols.basic import LineReceiver<br>
<br>class TelnetEcho(StatefulTelnetProtocol):<br>    def connectionMade(self):<br>        self.factory.connection.append(self)<br>        self.sendLine(&quot;Welcome to SimpleServer5\r\n&quot;)<br><br><br>    def lineReceived(self, data):<br>
        data = data.rstrip(&#39;\n\r&#39;)<br><br>        if data.upper() == &#39;BYE&#39;:<br>            self.sendLine(&quot;Goodbye...\r&quot;)<br>            self.transport.loseConnection()<br>        else:<br>            self.sendLine(&quot;Unrecognized command: %r\r&quot; % (data,))<br>
<br>    def connectionLost(self, reason):<br>        self.factory.connection.remove(self)<br><br><br>class TelnetEchoFactory(protocol.Factory):<br>    protocol = TelnetEcho<br>    def __init__(self):<br>        self.connection = []<br>
<br><br>def createTelnetServer(port=7300):<br>    telnetinstance = TelnetEchoFactory()       #  needs to be a list<br>    reactor.listenTCP(port,telnetinstance)<br><br>if __name__ == &quot;__main__&quot;:<br>    reactor.callWhenRunning(createTelnetServer)<br>
    reactor.run()<br><br>