免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5365 | 回复: 11
打印 上一主题 下一主题

Python twisted的疑问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-05-19 10:04 |只看该作者 |倒序浏览
我最近在学习twisted但是看着官方的文档有些问题,请教下大家,下面是2个简单的实例
  1. from twisted.internet.protocol import Protocol

  2. class Echo(Protocol):

  3.     def dataReceived(self, data):
  4.         self.transport.write(data)
复制代码
这是一个简单的消息接受的类,重写了Protocol中的dataReceived,这里我有个疑问了,我查看了源代码中的Protocol类,并未发现有write这个属性,为何这里要这样写?这样的代码运行期也是错误的,就是报错没有write属性。为什么啊。!!!!
下面的Protocol代码
  1. class BaseProtocol:
  2.     """
  3.     This is the abstract superclass of all protocols.

  4.     Some methods have helpful default implementations here so that they can
  5.     easily be shared, but otherwise the direct subclasses of this class are more
  6.     interesting, L{Protocol} and L{ProcessProtocol}.
  7.     """
  8.     connected = 0
  9.     transport = None

  10.     def makeConnection(self, transport):
  11.         """Make a connection to a transport and a server.

  12.         This sets the 'transport' attribute of this Protocol, and calls the
  13.         connectionMade() callback.
  14.         """
  15.         self.connected = 1
  16.         self.transport = transport
  17.         self.connectionMade()

  18.     def connectionMade(self):
  19.         """Called when a connection is made.

  20.         This may be considered the initializer of the protocol, because
  21.         it is called when the connection is completed.  For clients,
  22.         this is called once the connection to the server has been
  23.         established; for servers, this is called after an accept() call
  24.         stops blocking and a socket has been received.  If you need to
  25.         send any greeting or initial message, do it here.
  26.         """

  27.                                                                
  28. connectionDone=failure.Failure(error.ConnectionDone())
  29. connectionDone.cleanFailure()


  30. @implementer(interfaces.IProtocol, interfaces.ILoggingContext)
  31. class Protocol(BaseProtocol):
  32.     """
  33.     This is the base class for streaming connection-oriented protocols.

  34.     If you are going to write a new connection-oriented protocol for Twisted,
  35.     start here.  Any protocol implementation, either client or server, should
  36.     be a subclass of this class.

  37.     The API is quite simple.  Implement L{dataReceived} to handle both
  38.     event-based and synchronous input; output can be sent through the
  39.     'transport' attribute, which is to be an instance that implements
  40.     L{twisted.internet.interfaces.ITransport}.  Override C{connectionLost} to be
  41.     notified when the connection ends.

  42.     Some subclasses exist already to help you write common types of protocols:
  43.     see the L{twisted.protocols.basic} module for a few of them.
  44.     """

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

  51.     def dataReceived(self, data):
  52.         """Called whenever data is received.

  53.         Use this method to translate to a higher-level message.  Usually, some
  54.         callback will be made upon the receipt of each complete protocol
  55.         message.

  56.         @param data: a string of indeterminate length.  Please keep in mind
  57.             that you will probably need to buffer some data, as partial
  58.             (or multiple) protocol messages may be received!  I recommend
  59.             that unit tests for protocols call through to this method with
  60.             differing chunk sizes, down to one byte at a time.
  61.         """

  62.     def connectionLost(self, reason=connectionDone):
  63.         """Called when the connection is shut down.

  64.         Clear any circular references here, and any external references
  65.         to this Protocol.  The connection has been closed.

  66.         @type reason: L{twisted.python.failure.Failure}
  67.         """
复制代码

论坛徽章:
7
荣誉版主
日期:2011-11-23 16:44:17子鼠
日期:2014-07-24 15:38:07狮子座
日期:2014-07-24 11:00:54巨蟹座
日期:2014-07-21 19:03:10双子座
日期:2014-05-22 12:00:09卯兔
日期:2014-05-08 19:43:17卯兔
日期:2014-08-22 13:39:09
2 [报告]
发表于 2014-05-19 10:07 |只看该作者
这个例子不完整啊,没有协议工厂也没有reactor

论坛徽章:
0
3 [报告]
发表于 2014-05-19 10:16 |只看该作者
恩,我知道不是一个完整的例子,transport确实没有write属性,这让我很难理解。我再试试完整的例子。

论坛徽章:
0
4 [报告]
发表于 2014-05-19 10:19 |只看该作者
这个是比较完整的实例  还是有不理解的地方
  1. from twisted.internet.protocol import Factory, Protocol
  2. from twisted.internet.endpoints import TCP4ServerEndpoint
  3. from twisted.internet import reactor

  4. class QOTD(Protocol):

  5.     def connectionMade(self):
  6.         # self.factory was set by the factory's default buildProtocol:
  7.         self.transport.write(self.factory.quote + '\r\n')                      <==================真心搞不懂这里,代码如何能正确运行
  8.         self.transport.loseConnection()


  9. class QOTDFactory(Factory):

  10.     # This will be used by the default buildProtocol to create new protocols:
  11.     protocol = QOTD                   <=====================这样定义不会有错?

  12.     def __init__(self, quote=None):
  13.         self.quote = quote or 'An apple a day keeps the doctor away'

  14. endpoint = TCP4ServerEndpoint(reactor, 8007)
  15. endpoint.listen(QOTDFactory("configurable quote"))
  16. reactor.run()
复制代码

论坛徽章:
0
5 [报告]
发表于 2014-05-19 10:20 |只看该作者
回复 2# r2007

能帮我看看吗?


   

论坛徽章:
0
6 [报告]
发表于 2014-05-19 10:36 |只看该作者
谢谢,似乎只要调用了factory上面一切错误都被纠正了。。。。。。。

论坛徽章:
7
荣誉版主
日期:2011-11-23 16:44:17子鼠
日期:2014-07-24 15:38:07狮子座
日期:2014-07-24 11:00:54巨蟹座
日期:2014-07-21 19:03:10双子座
日期:2014-05-22 12:00:09卯兔
日期:2014-05-08 19:43:17卯兔
日期:2014-08-22 13:39:09
7 [报告]
发表于 2014-05-19 10:45 |只看该作者
factory建立连接时会用protocol = QOTD这个变量值创建一个协议实例,并且将自己以及transport传给这个实例,所以协议就有了factory和transport的引用变量了。

论坛徽章:
0
8 [报告]
发表于 2014-05-19 10:48 |只看该作者
回复 7# r2007


    谢谢您的解答,我这里学习twisted比较费劲,不知道有什么好的方法学习这个框架呢?

论坛徽章:
7
荣誉版主
日期:2011-11-23 16:44:17子鼠
日期:2014-07-24 15:38:07狮子座
日期:2014-07-24 11:00:54巨蟹座
日期:2014-07-21 19:03:10双子座
日期:2014-05-22 12:00:09卯兔
日期:2014-05-08 19:43:17卯兔
日期:2014-08-22 13:39:09
9 [报告]
发表于 2014-05-19 10:57 |只看该作者
twisted官网文档,这个没办法学习曲线较陡。

论坛徽章:
0
10 [报告]
发表于 2014-05-19 11:02 |只看该作者
框架太大,很难看懂啊。。。。。。。。。各种继承,疯了都。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP