在这个blog上学习twisted。
http://blog.163.com/gzjck_gshock/blog/static/77206203201291461538770/
里面的一个关于DeferredList的例子一开始对语法不太理解,copy并run了一遍就理解了,把代码贴在这里留作以后借鉴。
connecttest.py:
from twisted.internet import reactor, defer, protocol
class CallbackAndDisconnectProtocol(protocol.Protocol):
def connectionMade(self):
self.factory.deferred.callback("Connected!")
self.transport.loseConnection( )
class ConnectionTestFactory(protocol.ClientFactory):
protocol = CallbackAndDisconnectProtocol
def __init__(self):
self.deferred = defer.Deferred( )
def clientConnectionFailed(self, connector, reason):
self.deferred.errback(reason)
def testConnect(host, port):
testFactory = ConnectionTestFactory( )
reactor.connectTCP(host, port, testFactory)
return testFactory.deferred
def handleSuccess(result, port):
print "Connected to port %i" % port
reactor.stop( )
def handleFailure(failure, port):
print "Error connecting to port %i: %s" % (
port, failure.getErrorMessage( ))
reactor.stop( )
if __name__ == "__main__":
import sys
if not len(sys.argv) == 3:
print "Usage: connectiontest.py host port"
sys.exit(1)
host = sys.argv[1]
port = int(sys.argv[2])
connecting = testConnect(host, port)
connecting.addCallback(handleSuccess, port)
connecting.addErrback(handleFailure, port)
reactor.run( )
portscan.py
from twisted.internet import reactor, defer
from connecttest import testConnect
def handleAllResults(results, ports):
for port, resultInfo in zip(ports, results):
print resultInfo
success, result = resultInfo
if success:
print "Connected to port %i" % port
reactor.stop( )
import sys
host = sys.argv[1]
ports = range(8000, 8010)
testers = [testConnect(host, port) for port in ports]
defer.DeferredList(testers, consumeErrors=True).addCallback(
handleAllResults, ports)
reactor.run( )
执行脚本连接localhost:
$python portscan.py 127.0.0.1
(False, <twisted.python.failure.Failure <class 'twisted.internet.error.ConnectionRefusedError'>>)
(False, <twisted.python.failure.Failure <class 'twisted.internet.error.ConnectionRefusedError'>>)
(False, <twisted.python.failure.Failure <class 'twisted.internet.error.ConnectionRefusedError'>>)
(False, <twisted.python.failure.Failure <class 'twisted.internet.error.ConnectionRefusedError'>>)
(False, <twisted.python.failure.Failure <class 'twisted.internet.error.ConnectionRefusedError'>>)
(False, <twisted.python.failure.Failure <class 'twisted.internet.error.ConnectionRefusedError'>>)
(False, <twisted.python.failure.Failure <class 'twisted.internet.error.ConnectionRefusedError'>>)
(True, 'Connected!')
Connected to port 8007
(False, <twisted.python.failure.Failure <class 'twisted.internet.error.ConnectionRefusedError'>>)
(False, <twisted.python.failure.Failure <class 'twisted.internet.error.ConnectionRefusedError'>>)
我将DeferredList中的每个Deferred打印出来就看懂handleAllResults()中的resultInfo是什么了。
分享到:
相关推荐
第一部分:Twisted理论基础 第二部分:异步编程初探与reactor模式 第三部分:初步认识Twisted 第四部分:由Twisted...第二十部分:轮子中的轮子: Twisted和Erlang 第二十一部分:惰性不是迟缓: Twisted和Haskell
twisted系列教程,中文翻译,很好的学习教程
twisted-intro-cn, 在中文中,异步编程和 Twisted的Dave介绍 异步编程和 Twisted 中的介绍本项目是"twisted与异步编程入门"系列文章的简体中文翻译。原文由Dave撰写,参见 krondo.com 。如果你是Twisted新手,...
传说中的最好的twisted入门翻译教程...感谢作者的无私奉献..
本文对最基础的twisted架构进行了介绍,其中重点讲解了reactor和deferred,读者能够对twisted核心有很好的认识
最近有人在twisted邮件列表中问有没有一个可以让人快速学习twisted的文档.总体的来说:这个系列不是这样的一个文档.如果你没有很多时间或者耐心的话,这个系列的文章不太适合你. 不过,如果你对异步编程了解很少的话,...
Twisted与异步编程入门,最好的入门资源。
Twisted Network Programming Essentials 2nd Edition BOOK DESCRIPTION Get started with Twisted, the event-driven networking framework written in Python. With this introductory guide, you’ll learn the...
Python Twisted模块 10.2.0Python Twisted模块 10.2.0Python Twisted模块 10.2.0Python Twisted模块 10.2.0Python Twisted模块 10.2.0Python Twisted模块 10.2.0
twisted资料twisted资料twisted资料
网络编程中我们越来越感觉twisted的强大,本教材是一本很好的twisted入门级教材
Twisted Info Twisted入门教程源码
Twisted 现在网上对旧版本已经停止下载了,把以前的备份放上,是独立安装包WHL 需要pip install wheel 更新一下
twisted.rar,Twisted-17.9.0.dist-info.rar:主要是Python虚拟环境中scrapy的框架容易安装失败的两个,可能时间久了,就会不适用!!
Twisted是一个事件驱动的python网络框架被所有类型的软件使用。
twisted 异步教程,比较全面的介绍了twisted框架,中文版本
安装步骤 tar -jxvf Twisted-17.5.0.tar.bz2 cd Twisted-17.5.0 python setup.py install
Twisted Network Programming Essentials,一本介绍socket异步编程框架twisted
Twisted诞生于2000年初,在当时的网络游戏开发者看来,无论他们使用哪种语言,手中都鲜有可兼顾扩展性及跨平台的网络库。Twisted的作者试图在当时现有的环境下开发游戏,这一步走的非常艰难,他们迫切地需要一个可...
Twisted诞生于2000年初,在当时的网络游戏开发者看来,无论他们使用哪种语言,手中都鲜有可兼顾扩展性及跨平台的网络库。Twisted的作者试图在当时现有的环境下开发游戏,这一步走的非常艰难,他们迫切地需要一个可...