<p></p><blockquote type="cite">On Feb 2, 2010 8:54 AM,  &lt;<a href="mailto:exarkun@twistedmatrix.com" target="_blank">exarkun@twistedmatrix.com</a>&gt; wrote:<br><br><p><font color="#500050">On 02:15 am, <a href="mailto:mebly5343@gmail.com" target="_blank">mebly5343@gmail.com</a> wrote:<br>


&gt;Good day, everyone:<br>&gt;<br>&gt;</font>&gt;Good day, everyone:<br>
&gt;<br>
&gt;I&#39;m trying to learn Python and Twisted at the same time and having fun<br>
&gt;(mostly).<br>
&gt;<br>
&gt;I&#39;m writing an application that is collecting data from multiple<br>
&gt;sources, filtering the data, and providing it to users through a Telnet server.<br>
&gt;I can set up a polling loop for the server but I can&#39;t figure out how to<br>
&gt;send the data to users connected to my server.</p>It&#39;s possible this FAQ entry will help:<br>


<br>
<a href="http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother" target="_blank">http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother</a><br>



<br>
Jean-Paul<br><br>Thanks, Jean-Paul.  The following appears to do exactly what I want, based on your FAQ entry.  It reads data from a socket<br>connection and sends it to clients connected to the Telnet server.  I think I&#39;ve captured the correct instances this time.  :-)<br>
<br>Now, I need to work oncollecting the data and play with tksupport to provide a GUI...!<br><br>Thanks again.<br><br>Mark<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 mySocket(LineReceiver):<br>    def connectionMade(self):<br>        self.factory.connection = self<br>        self.factory.data = []<br><br>    def lineReceived(self, line):<br>
        self.factory.data.append(line)<br><br>    def connectionLost(self, reason):<br>        self.factory.connection = None<br><br>class mySocketFactory(protocol.Factory):<br>    protocol = mySocket<br>    def __init__(self):<br>
        self.connection = None<br>        self.data = []<br><br><br>class TelnetEcho(StatefulTelnetProtocol):<br>    def connectionMade(self):<br>        self.factory.connection = self<br>        self.sendLine(&quot;Welcome to the ClusterMerge 0.9Beta 20100203 telnet port\r\n\narc &gt;\r&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>
            self.factory.connection = None<br>        else:<br>            self.sendLine(&quot;Unrecognized command: %r\r&quot; % (data,))  #  will implement help, set/filter, and show/filter later<br><br>    def connectionLost(self, reason):<br>
        self.factory.connection = None<br><br><br>class TelnetEchoFactory(protocol.Factory):<br>    protocol = TelnetEcho<br>    def __init__(self):<br>        self.connection = None<br><br><br>def checkforspots(telnetinstance, socketinstance):<br>
    while len(socketinstance.data) &gt; 0:<br>        line = socketinstance.data.pop()<br>        if telnetinstance.connection:<br>            telnetinstance.connection.sendLine(line + &quot;\r&quot;)<br><br>    reactor.callLater(1.0,checkforspots, telnetinstance, socketinstance)<br>
<br>def createTelnetServer(port=7300, myport=7301):<br>    telnetinstance = TelnetEchoFactory()<br>    reactor.listenTCP(port,telnetinstance)<br><br>    socketinstance = mySocketFactory()<br>    reactor.listenTCP(myport, socketinstance)<br>
<br>    checkforspots(telnetinstance, socketinstance)<br><br><br>if __name__ == &quot;__main__&quot;:<br>    port = 8023<br>    reactor.callWhenRunning(createTelnetServer, port)<br>    reactor.run()<br><br><br><br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com" target="_blank">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</blockquote><p></p>