【发布时间】:2015-12-02 22:01:34
【问题描述】:
我是扭曲的新手。我编写了一个客户端,它通过两个端口 8037 和 8038 连接到服务器。我知道工厂创建了两个连接对象。现在当我按下 Ctrl-C 时,它会显示
Connection Lost Connection to the other side was lost in a non clean fashion.
Connection Lost Connection to the other side was lost in a non clean fashion.
下面是代码:
from twisted.internet import protocol,reactor
class TestClient(protocol.Protocol):
def __init__(self):
pass
def connectionMade(self):
print "Connected "
self.sayHello()
def connectionLost(self,reason):
self.transport.loseConnection()
def sayHello(self):
self.transport.write("Hello")
def dataReceived(self,data):
print "Received data ",data
class TestClientFactory(protocol.ClientFactory):
def buildProtocol(self,addr):
return TestClient()
def clientConnectionFailed(self,connectory,reason):
print "Connection Failed ",reason.getErrorMessage()
def clientConnectionLost(self,connector,reason):
print "Connection Lost ",reason.getErrorMessage()
reactor.connectTCP("<server_ip>",8037,TestClientFactory())
reactor.connectTCP("<server_ip>",8038,TestClientFactory())
reactor.run()
- 如何让客户端干净地关闭两个 tcp 连接?
- 如何只为一个连接调用sayHello()方法?
我刚接触twisted,所以举个例子会很有帮助。
谢谢
【问题讨论】: