[Twisted-Python] How do I get data from a Queue (or "outside") into a Telnet Server?

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Tue Feb 2 08:47:43 EST 2010


On 02:15 am, mebly5343 at gmail.com wrote:
>Good day, everyone:
>
>I'm trying to learn Python and Twisted at the same time and having fun
>(mostly).
>
>I'm writing an application that is collecting data from multiple 
>sources,
>filtering the data, and providing it to users through a Telnet server. 
>I
>can set up a polling loop for the server but I can't figure out how to 
>get
>send the data to users connected to my server.

What about this specifically are you having difficulty with?  The 
solution should probably be little more than a normal Python method 
call.
>
>I thought I'd put the server in a thread and use a Queue to send data 
>to
>it.  But, I could do the work within the server application...

I'd skip the thread and the queue.  From what you've said so far, there 
doesn't seem to be any reason to involve either.
>
>The server I'm playing with, including the polling loop, is:
>
>from twisted.conch.telnet import StatefulTelnetProtocol
>from twisted.internet import reactor, protocol
>
>class TelnetEcho(StatefulTelnetProtocol):
>
>    def lineReceived(self, data):
>        print "Type of self: " + str(type(self))
>        data = data.rstrip('\n\r')
>        self.sendLine("Unrecognized command: %r\r" % (data,))
>
>
>def checkforspots():
>    print "running checkforspots()"
>    reactor.callLater(1.0,checkforspots)
>
>def createTelnetServer():
>    factory = protocol.ServerFactory()
>
>    instance = TelnetEcho
>    factory.protocol = instance

This isn't exactly wrong, but it's sort of misleading.  You've defined 
the name "instance" here, but it's not bound to an instance.  It's bound 
to the TelnetEcho class.  Since you go on to set "factory.protocol" to 
this, it's right - that attribute is supposed to be a class, not an 
instance - but calling this "instance" suggests you might be confused, 
or it might confuse someone later reading the code.
>
>    port = reactor.listenTCP(8023,factory)
>    print "Listening on port 8023"
>
>    print "Type of port: " + str(type(port))
>    checkforspots()
>
>    return port
>
>if __name__ == "__main__":
>    reactor.callWhenRunning(createTelnetServer)
>    reactor.run()
>
>
>Any suggestions greatly appreciated.  I've been unable to find anything
>using my "google-fu" on this issue - I'm stuck.

It's possible this FAQ entry will help:

http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother

Jean-Paul



More information about the Twisted-Python mailing list