简单协议拒绝另一端的连接



我的两个依赖的类方法开发了一个Protocol来定义连接策略和一个工厂来打开连接。我已经通过连接到本地主机和端口来启动反应器来运行此操作,但是我得到以下错误:

连接失败。原因:[failed instance: Traceback (Failure with no frames):: Connection was refused by other side: 61: Connection refused.

from twisted.internet import protocol, task, reactor
from twisted.internet.protocol import ClientFactory
from twisted.internet.endpoints import TCP4ClientEndpoint, connectProtocol
class TestClass(protocol.Protocol):
def __init__(self):
self._make_connection = self.transport.write("Connect to the transport")
self.cnt_lost = self.transport.loseConnection()
self._tst = self.transport.getPeer()

def test_transport(self):
self._make_connection
self._tst
self.cnt_lost
class EchoClientFactory(ClientFactory):
def startedConnecting(self, connector):
print('Started to connect.')
def buildProtocol(self, addr):
print('Connected.')
return TestClass()
def clientConnectionLost(self, connector, reason):
print('Lost connection.  Reason:', reason)
def clientConnectionFailed(self, connector, reason):
print('Connection failed. Reason:', reason)

reactor.connectTCP('127.0.0.1', 8050, EchoClientFactory())
reactor.run()

没有太多的答案,我设法解决了上面的问题,但是我没有从服务器返回任何数据,例如,我应该得到:

Connect to the transport

得到:

Connected

也没有,我没有远程访问连接。

这是我尝试过的:

监听连接

class TestClass(protocol.Protocol):
def recieved_data(self, data):
self.transport.write(data)

class readClientFactory(ClientFactory):
def buildProtocol(self, addr):
print('Connected.')
return TestClass()

reactor.listenTCP(8070, readClientFactory())
reactor.run()

连接端口:

class readClass(protocol.Protocol):
def connectionmade(self):
self.transport.write(b"Connect to the transport")
self.transport.getPeer()

def test_transport(self):
self.transport.loseConnection()
class readClientFactory(ClientFactory):
def buildProtocol(self, addr):
print('Connected.')
return readClass()
def clientConnectionLost(self, connector, reason):
print('Lost connection.  Reason:', reason)
reactor.stop()
def clientConnectionFailed(self, connector, reason):
print('Connection failed. Reason:', reason)
reactor.stop()

reactor.connectTCP('127.0.0.1', 8070, readClientFactory())
reactor.run()

输出:

Connected.

应该是:

Connected.
Connect to the transport
--- Then some stuff about the ip

最新更新