user176420
user176420

Reputation: 51

Send in async mode data using twisted in python

I want to send data to server in async mode (whenever I type something in console) not only one time as the below code do. Is there any protocol function within twisted library that can handle this? In the following find the code that only send a message where the connection is established. On the other hand I can receive data in async mode via the function dataReceived. Is any function that will allow me to send messages in async mode as dataReceived is for receiving.

from twisted.internet import reactor, protocol


class QuoteProtocol(protocol.Protocol):
    def __init__(self, factory):
        self.factory = factory
    def connectionMade(self):
        self.sendQuote()
    def sendQuote(self):
        self.message(self.factory.quote)
    def dataReceived(self, data):
        print "Received quote:", data
        #self.transport.loseConnection()

class QuoteClientFactory(protocol.ClientFactory):
    def __init__(self, quote):
        self.quote = quote
    def buildProtocol(self, addr):
        return QuoteProtocol(self)
    def clientConnectionFailed(self, connector, reason):
        print 'connection failed:', reason.getErrorMessage()
        reactor.stop()
    def clientConnectionLost(self, connector, reason):
        print 'connection lost:', reason.getErrorMessage()
        reactor.stop()

message = "hello world"
reactor.connectTCP('127.0.0.1', 5000, QuoteClientFactory())
reactor.run()

Upvotes: 1

Views: 129

Answers (1)

oberstet
oberstet

Reputation: 22011

If you want to asynchronously process keystrokes from a terminal, you might have a look at TerminalProtocol: http://twistedmatrix.com/documents/9.0.0/api/twisted.conch.insults.insults.TerminalProtocol.html

Upvotes: 1

Related Questions