我正在尝试在 Twisted 中编写一个简单的 Echo 客户端,它将键盘输入发送到服务器,并由用户自己输入“q”终止。简而言之,我只是想修改这个页面上的简单回显客户端(和变体) 。一点都不性感,只是基本款。
我正在为非常基本的事件循环而苦苦挣扎。看起来我无法启动/停止循环内的反应器,因为停止的反应器无法重新启动。如果我不停止反应器,那么我将永远无法进入下一行获得键盘输入。
让我的回声客户端工作的任何帮助将不胜感激。
from twisted.internet.protocol import ClientFactory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
class EchoClient(LineReceiver):
end="Bye-bye!"
def connectionMade(self):
#only write and end transmission if the message isn't empty
if len(self.factory.message) > 0:
self.sendLine(self.factory.message)
self.sendLine(self.end)
else:
#Else just terminate the connection
self.transport.loseConnection()
def lineReceived(self, line):
print "receive:", line
if line==self.end:
self.transport.loseConnection()
class EchoClientFactory(ClientFactory):
message = ""
def buildProtocol(self, address):
p = EchoClient()
p.factory = self
return p
def clientConnectionFailed(self, connector, reason):
reactor.stop()
def clientConnectionLost(self, connector, reason):
reactor.stop()
def main():
s = raw_input('Text to send (''q'' to terminate): ')
while s != 'q':
factory = EchoClientFactory()
factory.message = s
reactor.connectTCP('localhost', 8000, factory)
#This is bad because reactor cannot be restarted once it's been stopped
reactor.run()
s = raw_input('Text to send(''q'' to terminate): ')
if __name__ == '__main__':
main()