简体   繁体   中英

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()

如果要异步处理来自终端的击键,可以查看TerminalProtocolhttp : //twistedmatrix.com/documents/9.0.0/api/twisted.conch.insults.insults.TerminalProtocol.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM