【问题标题】:Twisted framework Python扭曲的框架 Python
【发布时间】:2014-12-13 06:40:19
【问题描述】:

我需要一些帮助才能得到一些东西。我是 OOP 的新手。从我在 Twisted 的文档中看到的,更准确地说是 Protocol 类:http://twistedmatrix.com/documents/current/api/twisted.internet.protocol.Protocol.html 我知道这个类就像定义了 3 个方法的接口,我应该覆盖这些方法并将代码放入其中,如以下经典示例所示:

from twisted.internet.protocol import Protocol

class Echo(Protocol):

    def dataReceived(self, data):
        self.transport.write(data) 

但是我没有得到也找不到的是,当 Protocol 类不包含实际代码,只是方法的定义时,这段代码是如何成功运行的。协议如何同时成为类和接口(它被创建为类但只包含方法的定义)?运行并响应事件以便调用定义的方法的实际代码在哪里?

【问题讨论】:

  • Twisted 的主要范式:don't call us, we'll call you。所以是的,抽象协议类不应该包含任何实际代码,只包含定义。你所说的“代码的主要部分”是Twisted.Reactor。但在真正需要之前,您不应该打扰自己,只需开始编写网络服务的实际部分,其余部分留给框架。
  • “运行成功”是什么意思?这个程序在运行时什么也不做。 “没有实际代码”是什么意思?有一行“实际”代码(我认为你的意思是“方法的实现”:self.transport.write(data)。“它被创建为类”是什么意思?为了让这个问题得到有用的回答,它必须缩小很多,并包括(例如)您为解决此问题而采取的步骤以及为什么它们不适合您。

标签: python twisted


【解决方案1】:

检查一下(从 gi​​thub 页面获取twisted):https://github.com/twisted/twisted/blob/trunk/twisted/internet/protocol.py

Protocol 是 BaseProtocol 的子类,它也在同一个 .py 文件中定义。

class Protocol(BaseProtocol):
"""
This is the base class for streaming connection-oriented protocols.
If you are going to write a new connection-oriented protocol for Twisted,
start here.  Any protocol implementation, either client or server, should
be a subclass of this class.
The API is quite simple.  Implement L{dataReceived} to handle both
event-based and synchronous input; output can be sent through the
'transport' attribute, which is to be an instance that implements
L{twisted.internet.interfaces.ITransport}.  Override C{connectionLost} to be
notified when the connection ends.
Some subclasses exist already to help you write common types of protocols:
see the L{twisted.protocols.basic} module for a few of them.
"""

def logPrefix(self):
    """
    Return a prefix matching the class name, to identify log messages
    related to this protocol instance.
    """
    return self.__class__.__name__


def dataReceived(self, data):
    """Called whenever data is received.
    Use this method to translate to a higher-level message.  Usually, some
    callback will be made upon the receipt of each complete protocol
    message.
    @param data: a string of indeterminate length.  Please keep in mind
        that you will probably need to buffer some data, as partial
        (or multiple) protocol messages may be received!  I recommend
        that unit tests for protocols call through to this method with
        differing chunk sizes, down to one byte at a time.
    """

def connectionLost(self, reason=connectionDone):
    """Called when the connection is shut down.
    Clear any circular references here, and any external references
    to this Protocol.  The connection has been closed.
    @type reason: L{twisted.python.failure.Failure}
    """

【讨论】:

    猜你喜欢
    • 2011-03-13
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多