美文网首页
twisted-client-3 简单理解

twisted-client-3 简单理解

作者: 勿忘心安_e3e4 | 来源:发表于2018-04-12 12:23 被阅读0次

最近在看twisted,直接在gitbook中找了一本twisted的书籍,虽然书中对代码进行了详细的解释,但是其中的代码不读个三四遍仍然难以理解。故在此把自己的理解记录下来。
先直接上代码与注释。

# This is the Twisted Get Poetry Now! client, version 3.0.

# NOTE: This should not be used as the basis for production code.

import optparse

from twisted.internet.protocol import Protocol, ClientFactory


def parse_args():
    # 这个函数不需要深究,就是在处理输入参数。只需要知道它返回了host和port就可以了。
    usage = """usage: %prog [options] [hostname]:port ...

This is the Get Poetry Now! client, Twisted version 3.0
Run it like this:

  python get-poetry-1.py port1 port2 port3 ...

If you are in the base directory of the twisted-intro package,
you could run it like this:

  python twisted-client-3/get-poetry-1.py 10001 10002 10003

to grab poetry from servers on ports 10001, 10002, and 10003.

Of course, there need to be servers listening on those ports
for that to work.
"""

    parser = optparse.OptionParser(usage)

    _, addresses = parser.parse_args()

    if not addresses:
        print(parser.format_help())
        parser.exit()

    def parse_address(addr):
        if ':' not in addr:
            host = '127.0.0.1'
            port = addr
        else:
            host, port = addr.split(':', 1)

        if not port.isdigit():
            parser.error('Ports must be integers.')

        return host, int(port)

    return list(map(parse_address, addresses))


class PoetryProtocol(Protocol):
    # 自定义接收协议
    poem = ''

    def dataReceived(self, data): # 自动接收数据 在data中。
        self.poem += data.decode('utf-8')

    def connectionLost(self, reason): # 连接断开后要做的事情,此处是调用了PoetryClientFactory的实例中的poem_finished方法。传入了poem
        # 可以看出 self.callback(poem)只有poem接收完成后才会调用。而这个callback实际上是传入的get_poem函数。
        # 也就是说实际上的回调函数是 get_poem()
        self.poemReceived(self.poem)

    def poemReceived(self, poem):
        self.factory.poem_finished(poem)


class PoetryClientFactory(ClientFactory):

    protocol = PoetryProtocol

    def __init__(self, callback):
        # 传入一个回调函数
        self.callback = callback

    def poem_finished(self, poem):
        # 文本接收完毕后调用该函数。
        self.callback(poem)


def get_poetry(host, port, callback):
    """
    Download a poem from the given host and port and invoke

      callback(poem)

    when the poem is complete.
    """
    from twisted.internet import reactor
    factory = PoetryClientFactory(callback)
    reactor.connectTCP(host, port, factory)


def poetry_main():
    addresses = parse_args()

    from twisted.internet import reactor

    poems = []

    def got_poem(poem):
        poems.append(poem)
        if len(poems) == len(addresses):
            reactor.stop()

    for address in addresses:
        host, port = address
        get_poetry(host, port, got_poem)

    reactor.run()

    for poem in poems:
        print(poem)


if __name__ == '__main__':
    poetry_main()

首先,该段代码对参数进行了处理,将所有的address以list的方式保存倒了addresses。
定义回调函数get_poem,当所有的poem接收完毕后,将其保存在poems中,并且当接收的诗歌数量和address的数量相等的时候,关闭reactor循环。
下面,按照我的理解,相当于将addresses中的address注册进reacotr循环中。采用tcp连接的方式,PoetryProtocol协议进行接收数据。

相关文章

  • twisted-client-3 简单理解

    最近在看twisted,直接在gitbook中找了一本twisted的书籍,虽然书中对代码进行了详细的解释,但是其...

  • 简单理解

    没钱的时候才来学理财,学会理财却沒钱理财,只能根据理财知识进行推演,好尴尬。 理财是讲的是如何投资和规避风险的问题...

  • CodeIgniter简单理解

    作者:烨竹 学习起因:公司老是让我做小需求,所以想学习一门较为轻量级的php框架,自己搭建的MVC框架老是出问题,...

  • currentHashMap简单理解

    currentHashMap结构

  • javaBean 简单理解

    JavaBean简单及使用 先说一下javaBean的相关概念 PO:persistant object持久对象,...

  • WorkerMan简单理解

    WorkerMan基本内容 入口文件 入口在根目录下定义一个.php文件 引入命名空间 namespace ...

  • js this简单理解

    先看一段简单代码 从结果上看1,3,6输出是一样的,2,4,5是一样的从调用角度理解,其实方法调用和函数式调用都是...

  • EOS简单理解

    未经本人同意,禁止转载 EOS是什么 EOS (Enterprise Operation System),企业操作...

  • AutoreleasePool简单理解

    根据网上看的博客,做了一下整理,更便于理解 AutoreleasePool 一、基本概念 ARC下的产物,为了替代...

  • http简单理解

    本文参考:https://www.jianshu.com/p/f14d3549783e HTTP简介 HTTP协议...

网友评论

      本文标题:twisted-client-3 简单理解

      本文链接:https://www.haomeiwen.com/subject/dzexkftx.html