前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python twisted 回调函数

python twisted 回调函数

作者头像
用户5760343
发布2022-05-14 12:49:41
6570
发布2022-05-14 12:49:41
举报
文章被收录于专栏:sktjsktj

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

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

import optparse, sys

from twisted.internet.protocol import Protocol, ClientFactory

def parse_args(): usage = """usage: %prog [options] [hostname]:port ... This is the Get Poetry Now! client, Twisted version 3.1 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. """

代码语言:javascript
复制
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 map(parse_address, addresses)

class PoetryProtocol(Protocol):

代码语言:javascript
复制
poem = ''

def dataReceived(self, data):
    self.poem += data

def connectionLost(self, reason):
    self.poemReceived(self.poem)

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

class PoetryClientFactory(ClientFactory):

代码语言:javascript
复制
protocol = PoetryProtocol

def __init__(self, callback, errback):
    self.callback = callback
    self.errback = errback

def poem_finished(self, poem):
    self.callback(poem)

def clientConnectionFailed(self, connector, reason):
    self.errback(reason)

def get_poetry(host, port, callback, errback): """ Download a poem from the given host and port and invoke callback(poem) when the poem is complete. If there is a failure, invoke: errback(err) instead, where err is a twisted.python.failure.Failure instance. """ from twisted.internet import reactor factory = PoetryClientFactory(callback, errback) reactor.connectTCP(host, port, factory)

def poetry_main(): addresses = parse_args()

代码语言:javascript
复制
from twisted.internet import reactor

poems = []
errors = []

def got_poem(poem):
    poems.append(poem)
    poem_done()

def poem_failed(err):
    print >>sys.stderr, 'Poem failed:', err
    errors.append(err)
    poem_done()

def poem_done():
    if len(poems) + len(errors) == len(addresses):
        reactor.stop()

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

reactor.run()

for poem in poems:
    print poem

if name == 'main': poetry_main()

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • This is the Twisted Get Poetry Now! client, version 3.1.
  • NOTE: This should not be used as the basis for production code.
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档