【问题标题】:twisted python self.transport is not working just after connecting to server连接到服务器后,扭曲的 python self.transport 无法正常工作
【发布时间】:2013-10-05 08:48:38
【问题描述】:

我一直在研究基于 kivy 和扭曲框架的P2P chat application,我希望能有所了解,但我遇到了这个问题,如果客户端需要连接到另一个客户端(通过其服务器) ) 它需要执行一种握手,

现在第一步是连接到客户端;

conn = reactor.connectTCP(host, port, CommCoreClientFactory(self))

然后写入连接;

conn.transport.write("data..\r\n")

这里连接成功但是线路不通,上面的代码我已经按照我的意图进行了压缩,请看comm/twisscomm中的add_peer_to_swarm(self, pid, host)方法.py

我的 clientProtocol/Factory 和 serverProtocol/Factory 代码可以在下面找到; (它们可以在 comm/commcoreclient.py 和 comm/commcoreserver.py 中找到。)

客户端协议

class CommCoreClientProtocol(LineReceiver):

"""
Communications core client protocol code.
"""

def __init__(self, factory):

    self._peer_host = None
    self._peer_port = None
    self._peer_repr = None
    self.factory    = factory

def connectionMade(self):
    "Run when connection is established with server."

    self._peer_host = self.transport.getPeer().host
    self._peer_port = self.transport.getPeer().port
    self._peer_repr = self._peer_host + " on " + str(self._peer_port)

    Logger.debug(
        "Connection success! Connected to {}".format(self._peer_repr))

def connectionLost(self, reason):
    "Run when connection is lost with server."

    Logger.warn("Lost connection with peer {}".format(self._peer_repr))

def lineReceived(self, line):
    "Run when response is recieved from server."

    response = self.factory.app.handle_response(line)

    if response:
        print response

    Logger.debug("Recieved : {}".format(base64.b64encode(line)))

客户端工厂

class CommCoreClientFactory(protocol.ReconnectingClientFactory):

protocol = CommCoreClientProtocol

def __init__(self, app):

    self.app = app

def startedConnecting(self, connector):
    "Run when initiaition of connection takes place."

    Logger.debug("Attempting connection...")

def buildProtocol(self, addr):
    "Build protocol on successful connection."

    Logger.debug("Connected.")
    Logger.debug("Resetting reconnection delay.")

    # Reset the delay on connection success
    self.resetDelay()

    # Overridden build protocol
    #client_protocol = self.protocol()
    #client_protocol.factory = self
    #return client_protocol

    return CommCoreClientProtocol(self)

def clientConnectionLost(self, connector, reason):
    "Run when connection with server is lost."

    #self.app.print_message("connection lost")
    Logger.debug("Lost connection: {}".format(reason.getErrorMessage()))

    return protocol.ReconnectingClientFactory.clientConnectionLost(
        self, connector, reason
    )

def clientConnectionFailed(self, connector, reason):
    "Run when attempt to connect with server fails."

    #self.app.print_message("connection failed")
    Logger.debug("Connection failed. {}".format(reason.getErrorMessage()))

    return protocol.ReconnectingClientFactory.clientConnectionFailed(
        self, connector, reason
    )

服务器协议

class CommCoreServerProtocol(LineReceiver):

"Server backend to pocess the commands"

def __init__(self):

    self._peer_host = None
    self._peer_port = None
    self._peer_repr = None

def connectionMade(self):
    "Run when connection is established with server."

    self._peer_host = self.transport.getPeer().host
    self._peer_port = self.transport.getPeer().port
    self._peer_repr = self._peer_host + " on " + str(self._peer_port)

    Logger.debug(
        "Connection success! Connected to {}".format(self._peer_repr))

def connectionLost(self, reason):
    "Run when connection is lost with server."

    Logger.error("Lost connection with peer {}".format(self._peer_repr))

def lineReceived(self, line):

    print "REVCD LINE!", line

    response = self.factory.app.handle_recieved_data(line)

    if response:
        #self.transport.write(response)
        self.sendLine(response)

服务器工厂

class CommCoreServerFactory(protocol.Factory):

protocol = CommCoreServerProtocol

def __init__(self, app):

    self.app = app

(请原谅伪劣的缩进!)

我想知道我可能出错的地方。 另外,如果您有兴趣,我已经提交了此issue。如果您浏览我的代码(comm/twiscomm.py),您会发现有些事情可能无法在服务器端完全正常工作,尤其是使用 handle_received_data() 时,但由于未收到数据,因此甚至不会调用它。

【问题讨论】:

    标签: python chat twisted p2p kivy


    【解决方案1】:

    client howto 解释并演示了如何使用 Twisted 的网络客户端 API。

    reactor.connectTCP 的 API 文档还告诉您有关其返回值的信息 - IConnector - 特别是缺少任何 transport 属性的接口。

    【讨论】:

    • 对不起,我的意思是说 conn.transport.write("data\r\n")。进行了更正。感谢您的快速回复。
    • 我已经更新了答案以反映您更改的问题:-)。
    • +1 看来我的基本理解是有缺陷的,传输只能通过协议类访问。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多