繁体   English   中英

使用Twisted in python发送异步模式数据

[英]Send in async mode data using twisted in python

我想以异步模式(无论何时在控制台中键入内容)将数据发送到服务器,而不仅仅是一次以下代码。 扭曲库中是否有任何协议功能可以处理此问题? 在下面找到仅在建立连接的情况下发送消息的代码。 另一方面,我可以通过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()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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