【问题标题】:Send in async mode data using twisted in python在 python 中使用 twisted 以异步模式发送数据
【发布时间】:2015-02-19 13:09:41
【问题描述】:

我想以异步模式(每当我在控制台中键入内容时)将数据发送到服务器,而不仅仅是一次,就像下面的代码一样。扭曲库中是否有任何协议函数可以处理这个问题?在下面找到仅在建立连接时发送消息的代码。另一方面,我可以通过函数 dataReceived 以异步模式接收数据。是否有任何功能可以让我以异步模式发送消息,因为 dataReceived 用于接收。

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

【问题讨论】:

    标签: python networking twisted


    【解决方案1】:

    如果你想从终端异步处理击键,你可以看看TerminalProtocolhttp://twistedmatrix.com/documents/9.0.0/api/twisted.conch.insults.insults.TerminalProtocol.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 2013-11-03
      相关资源
      最近更新 更多