<p></p><blockquote type="cite">On Feb 2, 2010 8:54 AM, <<a href="mailto:exarkun@twistedmatrix.com" target="_blank">exarkun@twistedmatrix.com</a>> 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>
>Good day, everyone:<br>><br>></font>>Good day, everyone:<br>
><br>
>I'm trying to learn Python and Twisted at the same time and having fun<br>
>(mostly).<br>
><br>
>I'm writing an application that is collecting data from multiple<br>
>sources, filtering the data, and providing it to users through a Telnet server.<br>
>I can set up a polling loop for the server but I can't figure out how to<br>
>send the data to users connected to my server.</p>It'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'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("Welcome to the ClusterMerge 0.9Beta 20100203 telnet port\r\n\narc >\r")<br>
<br><br> def lineReceived(self, data):<br> data = data.rstrip('\n\r')<br><br> if data.upper() == 'BYE':<br> self.sendLine("Goodbye...\r")<br> self.transport.loseConnection()<br>
self.factory.connection = None<br> else:<br> self.sendLine("Unrecognized command: %r\r" % (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) > 0:<br> line = socketinstance.data.pop()<br> if telnetinstance.connection:<br> telnetinstance.connection.sendLine(line + "\r")<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__ == "__main__":<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>