我想以异步方式(无论何时在控制台中键入内容)将数据发送到服务器,而不仅是一次,如下面的代码所示。扭曲库中是否有任何协议功能可以处理此问题?在下面找到仅在建立连接的情况下发送消息的代码。另一方面,我可以通过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()
最佳答案
如果要异步处理来自终端的击键,可以查看TerminalProtocol
:http://twistedmatrix.com/documents/9.0.0/api/twisted.conch.insults.insults.TerminalProtocol.html
关于python - 使用Twisted in python发送异步模式数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28607280/