我试图编写一个简单的Echo客户端在扭曲,发送键盘输入到服务器,并由用户输入'q'自己终止。总之,我只是想修改在this page上找到的简单回声客户端(和变体)。没有什么性感,只是基本。简单的扭曲回声客户端
我很苦恼与非常基本的事件循环。看起来我无法在回路内启动/停止反应堆,因为停止的反应堆不能为restarted。如果我不停止反应堆,那么我将永远不会进入下一行获得键盘输入。
任何帮助让我的回声客户端工作将不胜感激。
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()
可能重复的[python扭曲stdio多个连接到服务器与交互命令提示符](http://stackoverflow.com/questions/2311844/python-twisted-stdio-multiple-connections-to-a-server -with-a-command-prompt-for) – 2012-04-28 12:41:19
这不是真的重复,jbreicis提供的答案远远好于在那里提供的答案。 – CadentOrange 2012-04-28 13:47:02
我的回答对于特定的任务并不好。随着我的回答,我试图更多地说明如何使传统的阻止代码兼容twisted.However - deferToThread是最简单和最丑陋的方式。 Usualy你应该能够找到“原生”扭曲的替代品。至于线路输入,扭曲的示例部分提供了更好的扭曲本地方法http://twistedmatrix.com/trac/browser/trunk/doc/core/examples/stdin.py – jbreicis 2012-04-28 13:59:05