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

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.
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...
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
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.
Thanks.
Mark Bailey

On 02:15 am, mebly5343@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#HowdoImakeinputo...
Jean-Paul

Good day, Jean-Paul:
On Tue, Feb 2, 2010 at 8:47 AM, exarkun@twistedmatrix.com wrote:
On 02:15 am, mebly5343@gmail.com wrote: [...]
What about this specifically are you having difficulty with?
Actually, what I was having trouble with was getting an instance of the protocol class. (The "instance" was from a debugging line I forgot to take back out.) The FAQ entry does answer my questions (which I didn't understand well enough to ask clearly:-) ):
How do I get access to an instance of the protocol class? (The answer is to derive a class from Factory and use that to store "self" from the protocol class. The FAQ entry shows exactly how to do this through the __init__ method - thanks.)
Thanks also for steering me away from complexities like threads and Queue. callWhenRunning allows me to run in the context of the reactor and access other Python objects, so that solves my problem.
I will probably end up using a socket for that connection and running the server as a seperate process - but that is not the key.
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. [...]
It's possible this FAQ entry will help:
http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputo...
Jean-Paul
Thanks again for the very prompt and useful response.
Mark

On Feb 2, 2010 8:54 AM, exarkun@twistedmatrix.com wrote:
On 02:15 am, mebly5343@gmail.com wrote:
Good day, everyone:
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 send the data to users connected to my server.
It's possible this FAQ entry will help:
http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputo...
Jean-Paul
Thanks, Jean-Paul. The following appears to do exactly what I want, based on your FAQ entry. It reads data from a socket connection and sends it to clients connected to the Telnet server. I think I've captured the correct instances this time. :-)
Now, I need to work oncollecting the data and play with tksupport to provide a GUI...!
Thanks again.
Mark
from twisted.conch.telnet import StatefulTelnetProtocol from twisted.internet import reactor, protocol from twisted.protocols.basic import LineReceiver
class mySocket(LineReceiver): def connectionMade(self): self.factory.connection = self self.factory.data = []
def lineReceived(self, line): self.factory.data.append(line)
def connectionLost(self, reason): self.factory.connection = None
class mySocketFactory(protocol.Factory): protocol = mySocket def __init__(self): self.connection = None self.data = []
class TelnetEcho(StatefulTelnetProtocol): def connectionMade(self): self.factory.connection = self self.sendLine("Welcome to the ClusterMerge 0.9Beta 20100203 telnet port\r\n\narc >\r")
def lineReceived(self, data): data = data.rstrip('\n\r')
if data.upper() == 'BYE': self.sendLine("Goodbye...\r") self.transport.loseConnection() self.factory.connection = None else: self.sendLine("Unrecognized command: %r\r" % (data,)) # will implement help, set/filter, and show/filter later
def connectionLost(self, reason): self.factory.connection = None
class TelnetEchoFactory(protocol.Factory): protocol = TelnetEcho def __init__(self): self.connection = None
def checkforspots(telnetinstance, socketinstance): while len(socketinstance.data) > 0: line = socketinstance.data.pop() if telnetinstance.connection: telnetinstance.connection.sendLine(line + "\r")
reactor.callLater(1.0,checkforspots, telnetinstance, socketinstance)
def createTelnetServer(port=7300, myport=7301): telnetinstance = TelnetEchoFactory() reactor.listenTCP(port,telnetinstance)
socketinstance = mySocketFactory() reactor.listenTCP(myport, socketinstance)
checkforspots(telnetinstance, socketinstance)
if __name__ == "__main__": port = 8023 reactor.callWhenRunning(createTelnetServer, port) reactor.run()
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (2)
-
exarkun@twistedmatrix.com
-
Mark Bailey