上面是twisted官网推荐的书籍,从封面可以看到,是一堆大蟒(python)纠缠在一起,这里可以说明twisted是一个基于pyhton语言,支持各种网络协议(包括UDP,TCP,TLS和其他应用层协议,比如HTTP,SMTP,NNTM,IRC,XMPP/Jabber。)使用(纠缠)的较低层的通信框架,twisted框架已经实现了这些协议,我们只需要使用就行了。另外twisted框架是基于事件驱动
 
下面是搬运官网的例子:
 
TCP服务器
from twisted.internet import protocol, reactor, endpoints class Echo(protocol.Protocol):
def dataReceived(self, data):
self.transport.write(data)
class EchoFactory(protocol.Factory):
def buildProtocol(self, addr):
return Echo() endpoints.serverFromString(reactor, "tcp:1234").listen(EchoFactory()) reactor.run()

  

web服务器
from twisted.web import server, resource
from twisted.internet import reactor, endpoints class Counter(resource.Resource):
isLeaf = True
numberRequests = 0
def render_GET(self, request):
self.numberRequests += 1
request.setHeader(b"content-type", b"text/plain")
content = u"I am request #{}\n".format(self.numberRequests)
return content.encode("ascii") endpoints.serverFromString(reactor, "tcp:8080").listen(server.Site(Counter())) reactor.run()
聊天室
from twisted.internet import reactor, protocol, endpoints
from twisted.protocols import basic class PubProtocol(basic.LineReceiver):
def __init__(self, factory):
self.factory = factory def connectionMade(self):
self.factory.clients.add(self) def connectionLost(self, reason):
self.factory.clients.remove(self) def lineReceived(self, line):
for c in self.factory.clients:
source = u"<{}> ".format(self.transport.getHost()).encode("ascii")
c.sendLine(source + line) class PubFactory(protocol.Factory):
def __init__(self):
self.clients = set() def buildProtocol(self, addr):
return PubProtocol(self) endpoints.serverFromString(reactor, "tcp:1025").listen(PubFactory())
reactor.run()

你可以使用打开两个命令行终端,使用 telnet localhost 1025, 然后在每个终端输入。

 
邮件客户端
twisted 包含了一个复杂的 imap4 客户端库
from __future__ import print_function

import sys

from twisted.internet import protocol, defer, endpoints, task
from twisted.mail import imap4
from twisted.python import failure @defer.inlineCallbacks
def main(reactor, username="alice", password="secret",
strport="tls:example.com:993"):
endpoint = endpoints.clientFromString(reactor, strport)
factory = protocol.Factory.forProtocol(imap4.IMAP4Client)
try:
client = yield endpoint.connect(factory)
yield client.login(username, password)
yield client.select('INBOX')
info = yield client.fetchEnvelope(imap4.MessageSet(1))
print('First message subject:', info[1]['ENVELOPE'][1])
except:
print("IMAP4 client interaction failed")
failure.Failure().printTraceback() task.react(main, sys.argv[1:])

总结

twisted 是一个封装了各个网络协议,基于事件循环驱动的框架。

1. 要使用twisted框架,需实现一个twisted Protocol类的客户端或服务器类,重写其dataReceived,

定义接受到对端数据时的处理方法。

2. 对于服务器类(客户端类),还要实现一个twisted Factory(ClientFactory)类,用来管理Protocol类,当有新的客户端连接时,框架调用

Factory.buildProtocol()返回Protocol类去处理相应连接。

3. 使用twisted  相应的listen/connect方法,监听/连接对端服务。

4. 最后,调用twisted的事件循环处理类reactor.run(),启动事件循环。

 
 
下面是twisted的关键术语:
BaseProtocol
twisted框架所有协议类的基类,其实现了两个关键方法,makeConnection和connectionMade。
makeConnection继承给子类,一般我们不使用此方法;
connctionsMade继承给子类,子类重写该方法。在客户端连接建立时调用,可以做一些鉴权之类的操作,也可以限制客户端的连接总数。

class BaseProtocol:
"""
This is the abstract superclass of all protocols. Some methods have helpful default implementations here so that they can
easily be shared, but otherwise the direct subclasses of this class are more
interesting, L{Protocol} and L{ProcessProtocol}.
"""
connected = 0
transport = None def makeConnection(self, transport):
"""Make a connection to a transport and a server. This sets the 'transport' attribute of this Protocol, and calls the
connectionMade() callback.
"""
self.connected = 1
self.transport = transport
self.connectionMade() def connectionMade(self):
"""Called when a connection is made. This may be considered the initializer of the protocol, because
it is called when the connection is completed. For clients,
this is called once the connection to the server has been
established; for servers, this is called after an accept() call
stops blocking and a socket has been received. If you need to
send any greeting or initial message, do it here.

  

Protocol
from twisted.internet.protocol import Protocol
twisted框架为基于TCP协议等上层协议定义的基类(基于UDP协议的是DatagramProtocol,from twisted.internet.protocol import Protocol),服务端和客户端都需继承此类。
其api十分简单,重写dataReceived方法支持客户端有数据发送时的逻辑;重写connectionLost方法,可以根据失败原因做一些错误处理。
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}

  

Factory
from twisted.internet.protocol import Factory
twisted中的Factory子类起到对Protocol类的管理作用,当有新的客户端连接时,框架会调用Factory.buildProtocol(),此时,用户需在此时将自定义的Protocol实例传进去,并且作为该函数的返回值。
 
ClientFactory
from twisted.internet.protocol import ClientFactory
实现客户端连接程序管理的工厂类,需用reactors.connectXXX(如connectTCP) 指定与使用哪种网络协议的服务器通信。
ClientFactory继承自Factory类,但是比Factory多了3个可以重写的事件响应函数,即startedConnecting()在建立连接时被调用;clientConnectionLost()在连接断开时被调用;
clientConnectionFailed()在连接建立失败时被调用。
 

Twisted 框架 初印象的更多相关文章

  1. Vue.js之初印象

    一.背景 MVVM模式,很多人在说在用,好吧,我落后了,我目前的项目木有用到MVVM模式的框架,vuejs,reactjs,angularjs,nonono,自己去捣鼓过ng,项目木有用到.实在不敢称 ...

  2. OpenCL学习笔记(一):摩尔定律,异构计算与OpenCL初印象

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld.  技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入. 关于摩尔定律: 摩尔定律19 ...

  3. [强化学习]Part1:强化学习初印象

    引入 智能 人工智能 强化学习初印象 强化学习的相关资料 经典书籍推荐:<Reinforcement Learning:An Introduction(强化学习导论)>(强化学习教父Ric ...

  4. AVFoundation 框架初探究(二)

    接着第一篇总结 系列第一篇地址:AVFoundation 框架初探究(一) 在第一篇的文章中,我们总结了主要有下面几个点的知识: 1.对AVFoundation框架整体的一个认识 2.AVSpeech ...

  5. Django初印象之视图(view)

    一.view的初印象 一个视图函数(类),简称视图.我们发起web请求时,返回的web响应.[大家约定成俗将视图放置在项目(project)或应用程序(app)目录中的名为views.py的文件中.] ...

  6. 初印象至Vue路由

    初印象系列为快速了解一门技术的内容,后续会推出本人应用这门技术时发现的一些认识. Vue路由和传统路由的区别: Vue路由主要是用来实现单页面应用内各个组件之间的切换,同样支持传递参数等功能.而传统路 ...

  7. Three 之 Animation 初印象

    Animation 初印象 动画效果 播放动画需要基本元素 AnimationMixer 一个对象所有动作的管理者 用于场景中特定对象的动画的播放器.一个对象可能有多个动作,Mixer 是用来管理所有 ...

  8. twisted框架的使用和应用?

    https://www.cnblogs.com/zhiyong-ITNote/archive/2017/08/14/7360442.html twisted网络框架的三个基础模块:Protocol, ...

  9. Twisted框架学习

    Twisted是用Python实现的基于事件驱动的网络引擎框架,是python中一个强大的异步IO库.理解twisted的一个前提是弄清楚twisted中几个核心的概念: reactor, Proto ...

随机推荐

  1. 【fiddler】抓取https数据失败,全部显示“Tunnel to......443”

    这个问题是昨天下午就一直存在的,知道今天上午才解决,很感谢“韬光养晦”. 问题描述:  按照网络上的教程,设置fiddler开启解密https的选项,同时fiddler的证书也是安装到系统中,但是抓取 ...

  2. yii2自定义json格式success,error跳转

    /** * ---------------------------------------------- * 操作成功跳转的快捷方法 * @access protected * @param stri ...

  3. README.md 编写

    Spring Boot Demo =========================== 该文件用来测试和展示书写README的各种markdown语法.GitHub的markdown语法在标准的ma ...

  4. 第16章:MongoDB-聚合操作--聚合管道--$project

    ①$project $project作用:修改文档的结构,可以用来重命名.增加或删除文档中的字段. 执行的规则如下: |- 普通列({成员 : 1 | true}):表示要显示的内容: |- “_id ...

  5. i2c_client 几种实例化方法

    http://blog.csdn.net/lugandong/article/details/48092397

  6. day3用户交互,格式化输出,数据类型,流程控制

    上节课复习: 1.运行python程序的三步骤:python test.py 1.先启动python解释器 2.将test.py的内容当作普通的字符读入内存 3.python解释器解释执行刚刚读入内存 ...

  7. spring-数据库操作组件的实现

    Ok,今天看了下spring数据库组件实现的源码,简要的做下笔记. 首先是JdbcTemplate,其实这个技术的实现很简单,就是一个模板类,把执行sql的主干流程都写好了,比如connection的 ...

  8. STL容器(C11)--unordered_set用法

    http://www.cplusplus.com/reference/unordered_set/

  9. QT源码查看001-QApplication和QCoreApplication

    QCoreApplication和QApplication的区别(1) QApplication这个类是继承QCoreApplication的,而QCoreApplication有继承QObject的 ...

  10. Javascript 对象创建多种方式 原型链

    一.对象创建 1.new Object 方式 直接赋上属性和方法 var obj = new Object(); obj.name = '娃娃'; obj.showName = function(){ ...