python 网络框架twisted基础学习及详细讲解

2023-05-16

twisted网络框架的三个基础模块:Protocol, ProtocolFactory, Transport.这三个模块是构成twisted服务器端与客户端程序的基本。

Protocol:Protocol对象实现协议内容,即通信的内容协议
ProtocolFactory: 是工厂模式的体现,在这里面生成协议
Transport: 是用来收发数据,服务器端与客户端的数据收发与处理都是基于这个模块

在windows中安装twisted需要先安装pywin32,自己去下载下就行。随后pip install twisted就会帮我们安装twisted以及zope。

我们结合一张图,以及一段程序来理解下twisted的基础实现:

然后我们首先看看服务器端程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# coding=utf-8
from  twisted.internet.protocol  import  Protocol
from  twisted.internet.protocol  import  Factory
from  twisted.internet.endpoints  import  TCP4ServerEndpoint
from  twisted.internet  import  reactor
 
 
clients  =  []
 
 
class  Spreader(Protocol):
     def  __init__( self , factory):
         self .factory  =  factory
 
     def  connectionMade( self ):
         self .factory.numProtocols  =  self .factory.numProtocols  +  1
         self .transport.write(
             "欢迎来到Spread Site, 你是第%s个客户端用户!\n"  %  ( self .factory.numProtocols)
         )
         print  "new connect: %d"  %  ( self .factory.numProtocols)
         clients.append( self )
 
     def  connectionLost( self , reason):
         self .factory.numProtocols  =  self .factory.numProtocols  -  1
         clients.remove( self )
         print  "lost connect: %d"  %  ( self .factory.numProtocols)
 
     def  dataReceived( self , data):
         if  data  = =  "close" :
             self .transport.loseConnection()
             for  client  in  clients:
                 if  client ! =  self :
                     client.transport.write(data)
         else :
             print  data
 
 
class  SpreadFactory(Factory):
     def  __init__( self ):
         self .numProtocols  =  0
 
     def  buildProtocol( self , addr):
         return  Spreader( self )
 
 
endpoint  =  TCP4ServerEndpoint(reactor,  8007 )
endpoint.listen(SpreadFactory())
reactor.run()

创建一个TCP的IPv4版本的终结点,随后就开始监听listen, 在这里我们传入协议工厂对象作为参数, 先看看我们自定义的工厂类SpreadFactory, 它派生自Factory, 我们看看这个类的源码(此时你需要有道词典了:) ):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
@implementer (interfaces.IProtocolFactory, interfaces.ILoggingContext)
@_oldStyle
class  Factory:
     """
     This is a factory which produces protocols.
 
     By default, buildProtocol will create a protocol of the class given in
     self.protocol.
     """
 
     # put a subclass of Protocol here:
     protocol  =  None
 
     numPorts  =  0
     noisy  =  True
 
     @classmethod
     def  forProtocol( cls , protocol,  * args,  * * kwargs):
         """
         Create a factory for the given protocol.
 
         It sets the C{protocol} attribute and returns the constructed factory
         instance.
 
         @param protocol: A L{Protocol} subclass
 
         @param args: Positional arguments for the factory.
 
         @param kwargs: Keyword arguments for the factory.
 
         @return: A L{Factory} instance wired up to C{protocol}.
         """
         factory  =  cls ( * args,  * * kwargs)
         factory.protocol  =  protocol
         return  factory
 
 
     def  logPrefix( self ):
         """
         Describe this factory for log messages.
         """
         return  self .__class__.__name__
 
 
     def  doStart( self ):
         """Make sure startFactory is called.
 
         Users should not call this function themselves!
         """
         if  not  self .numPorts:
             if  self .noisy:
                 _loggerFor( self ).info( "Starting factory {factory!r}" ,
                                       factory = self )
             self .startFactory()
         self .numPorts  =  self .numPorts  +  1
 
     def  doStop( self ):
         """Make sure stopFactory is called.
 
         Users should not call this function themselves!
         """
         if  self .numPorts  = =  0 :
             # this shouldn't happen, but does sometimes and this is better
             # than blowing up in assert as we did previously.
             return
         self .numPorts  =  self .numPorts  -  1
         if  not  self .numPorts:
             if  self .noisy:
                 _loggerFor( self ).info( "Stopping factory {factory!r}" ,
                                       factory = self )
             self .stopFactory()
 
     def  startFactory( self ):
         """This will be called before I begin listening on a Port or Connector.
 
         It will only be called once, even if the factory is connected
         to multiple ports.
 
         This can be used to perform 'unserialization' tasks that
         are best put off until things are actually running, such
         as connecting to a database, opening files, etcetera.
         """
 
     def  stopFactory( self ):
         """This will be called before I stop listening on all Ports/Connectors.
 
         This can be overridden to perform 'shutdown' tasks such as disconnecting
         database connections, closing files, etc.
 
         It will be called, for example, before an application shuts down,
         if it was connected to a port. User code should not call this function
         directly.
         """
 
 
     def  buildProtocol( self , addr):
         """
         Create an instance of a subclass of Protocol.
 
         The returned instance will handle input on an incoming server
         connection, and an attribute "factory" pointing to the creating
         factory.
 
         Alternatively, L{None} may be returned to immediately close the
         new connection.
 
         Override this method to alter how Protocol instances get created.
 
         @param addr: an object implementing L{twisted.internet.interfaces.IAddress}
         """
         =  self .protocol()
         p.factory  =  self
         return  p

在这里很重要的一个函数就是buildProtocol, 此函数就是在工厂模式中创建协议的.我们是基于基类Factory来实现这个函数的, 下面我们看看派生自Protocol的协议类Spread,Spread的__Init__参数中,我们给它传入的是自定义的SpreadFactory, 然后我们看下基类Protocol的源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@implementer (interfaces.IProtocol, interfaces.ILoggingContext)
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}
         """

而Protocol又是派生自BaseProtocol的,继续看这个类的源代码:


@_oldStyle
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.
        """

connectionDone=failure.Failure(error.ConnectionDone())
connectionDone.cleanFailure()  

可以看到,我们自定义的Spread不过是实现了基类的函数。接下来我们滚一边实现逻辑:
首先,我们定义一个列表clients,以便存储多个客户端的连接。当服务器端接收到了客户端的连接后,调用connectionMade函数,同时,我们给使用Transport客户端发送消息, 通知客户端我们已收到连接。当客户端连接失去的时候,我们调用ConnectionLost, 同时移除列表中的客户端连接, dataReceived函数来接受数据,当客户端传来"close"命令时,我们就主动关闭连接, 否则,我们就把data输出来。

看看客户端的代码:


# coding=utf-8
from twisted.internet.protocol import Protocol, ClientFactory
from twisted.internet import reactor
import threading
import time
import sys
import datetime


class Echo(Protocol):
    def __init__(self):
        self.connected = False

    def connectionMade(self):
        self.connected = True

    def connectionLost(self, reason):
        self.connected = False

    def dataReceived(self, data):
        print data.decode("utf-8")


class EchoClientFactory(ClientFactory):
    def __init__(self):
        self.protocol = None

    def startedConnecting(self, connector):
        print "Start to Connect..."

    def buildProtocol(self, addr):
        print "Connected..."
        self.protocol = Echo()
        return self.protocol

    def clientConnectionLost(self, connector, reason):
        print "Lost connection. Reason: ", reason

    def clientConnectionFailed(self, connector, reason):
        print "Connection is failed, Reason: ", reason


bStop = False


def routine(factory):
    while not bStop:
        if factory.protocol and factory.protocol.connected:
            factory.protocol.transport.write("hello, I'm %s %s" % (
                sys.argv[0], datetime.datetime.now()
            ))
            print sys.argv[0], datetime.datetime.now()
        time.sleep(5)


host = '127.0.0.1'
port = 8007
factory = EchoClientFactory()
reactor.connectTCP(host, port, factory)
threading.Thread(target=routine, args=(factory,)).start()
reactor.run()
bStop = True  

一开始我们建立TCP连接, 传入主机地址, 端口, 协议工厂对象作为参数,随后reactor.run挂起运行。
下面我们看看ClientFactory基类,因为我们自定义的协议工厂EchoClientFactory派生自它。源码:


class ClientFactory(Factory):
    """A Protocol factory for clients.

    This can be used together with the various connectXXX methods in
    reactors.
    """

    def startedConnecting(self, connector):
        """Called when a connection has been started.

        You can call connector.stopConnecting() to stop the connection attempt.

        @param connector: a Connector object.
        """

    def clientConnectionFailed(self, connector, reason):
        """Called when a connection has failed to connect.

        It may be useful to call connector.connect() - this will reconnect.

        @type reason: L{twisted.python.failure.Failure}
        """

    def clientConnectionLost(self, connector, reason):
        """Called when an established connection is lost.

        It may be useful to call connector.connect() - this will reconnect.

        @type reason: L{twisted.python.failure.Failure}
        """  

同样的,我们自定义的EchoClientFactory不过就是实现了基类中没有实现的函数,其中最重要的还是buildProtocol, 它为我们生成一个协议,下面看下我们自定义的协议类Echo, 基类源代码与上面的是一样的.
客户端的协议函数与服务器端的协议函数是一样的,在这里就不多说。

客户端的twisted模块讲完了,随后我们创建了一个线程去和服务器端通信, 并且定时发送, 当然,在这里我们为了以防万一,需要判断是否已经与服务器取得了连接,随后才发送消息.

大概讲了下基础部分,所有的代码都是来自《python高效开发实战》里的代码,在这里也向大家推荐这本书,学习twisted还有两个不错的教程,在最后我会发百度网盘共享。之所以写这篇基础的,就是为了能够理解高效开发实战里的最后一个项目: 用Twisted开发跨平台物联网消息网关。因为第一次实习就接触到了物联网通信,在工作时,滚了一遍项目的源代码(java写的,但我毕竟也是学了C#, .net两年的人, 看懂项目源码没压力, mvc orm都是与.net中的EF, MVC差不多, 不过就是语法有点不同),正好和书上的这个项目差不多,书上将服务器与客户端的通信协议指令都讲得很清楚。因此这是一本不容错过的好书, 也是学习, 精通twisted的最好途径

最后就是运行测试:

服务器端:

客户端:

twisted教程: http://pan.baidu.com/s/1dEBPGhN

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python 网络框架twisted基础学习及详细讲解 的相关文章

  • 前端传一个数组或者集合后台怎么接受

    34 id 34 34 1 34 34 answer 34 34 A 34 34 id 34 34 1 34 34 answer 34 34 A 34 这样的可以直接用一个 64 RequesBody List lt QuestionBac
  • Java8处理List的双层循环

    Java处理List的双层循环程序员经常遇到 xff0c 一般都是当两个List某个值满足某条件时候 xff0c 进行相应的处理 xff1b 1 list和map之间的相互转换 两个List对象当id相同的时候 注意是两个对象 而非两个集合
  • java如何抛出异常

    1 什么时候抛出异常 如果你觉得某些 问题 解决不了了 xff0c 那么你就可以抛出异常了 比如 xff0c 你在写一个service 其中在写到某段代码处 你发现可能会产生问题 xff0c 那么就请抛出异常吧 xff0c 相信我 xff0
  • 发送短信验证码过于频繁问题的解决

    1 对请求的接口做了一个限流的控制 2 利用到 AOP redis 定时器 3 在请求的congtroller层上加相应的注解就可以 具体的Demo工程如下 package com weigu xiaochuang project impo
  • spring的controller是单例还是多例

    我们经常说单例还是多例 那么究竟他们不同的根源在哪 或者说我们应该从哪一方面具体的去理解了 至于这个问题 今天做一个小的探讨 其实我们最终说的是 64 auowired注解的引入的service或mapper是不是单例还是多例的 这个是这个
  • 多线程-批量获取多条线程的执行结果

    当向线程池提交callable任务后 xff0c 我们可能需要一次性获取所有返回结果 xff0c 有三种处理方法 方法一 xff1a 自己维护返回结果 创建一个线程池 ExecutorService executorService 61 E
  • nautilus命令介绍

    nautilus 图形化桌面包括了一个叫做 Nautilus 的文件管理器 在GNOME中是Nautilus 鹦鹉螺 xff0c 而KDE中是Konqueror
  • 写个strcat函数

    include 34 stdio h 34 include lt string h gt void stracat char a char b char temp 128 char p 61 temp int alen 61 strlen
  • linux 下 tcp client的 demo

    include lt stdio h gt include lt stdlib h gt include lt string h gt include lt unistd h gt include lt sys socket h gt in
  • 解决MATLAB2020B关于找不到vs2019C++编译器问题

    在配置matlab 深度学习环境过程中 xff0c 出现找不到vs219C 43 43 编译器 问题 xff0c 尝试了各种办法 xff0c 还是失败 xff0c 经过摸索和结合他人经验 xff0c 最终成功 xff0c 予以总结 xff0
  • Ubuntu下使用w3m命令行模式浏览网页

    w3m是一个基于文本的网页浏览器 xff0c 支持多种操作系统 xff0c 在命令行终端可以很好的支持中文 即使在没有鼠标支持的情况下也可以检查网页的输出 我们一般用Ubuntu的X Windows来看图形界面的东西 xff0c 有没有想过
  • CC3D飞控说明书/使用手册

    CC3D飞控说明书 使用手册openpilot librepilot CC硬件配置 记得点赞哦 xff01 xff01 xff01
  • 串口服务器的原理及使用方法

    串口服务器是将来自TCP IP协议的数据包 xff0c 解析为串口数据流 xff1b 反之 xff0c 也可以将串口数据流打成TCP IP协议的数据包 xff0c 从而实现数据的网络传输 它能多个串口设备连接并能将串口数据流进行选择和处理
  • tcpdump命令

    tcpdump tcpdump命令介绍 tcpdump xff0c 用简单的语言概括就是dump the traffic on a network xff0c 是一个运行在linux平台可以根据使用者需求对网络上传输的数据包进行捕获的抓包工
  • 小程序登录后,接口调用失败返回401

    问题描述 xff1a 小程序登录后 xff0c 所有的内部接口不可调用 xff0c 全部授权失败 xff0c 接口返回401 xff1b 解决办法 xff1a 服务端清缓存就可以了 微信开发者工具中 xff0c 小程序多次登录 xff0c
  • 车牌识别算法 基于yolov5的车牌检测+crnn中文车牌识别 支持12种中文车牌识别

    yolov5 车牌识别算法 xff0c 支持12种中文车牌类型 基于yolov5的车牌检测 车牌矫正以及 基于CRNN的车牌识别 1 单行蓝牌 2 单行黄牌 3 新能源车牌 4 白色警用车牌 5 教练车牌 6 武警车牌 7 双层黄牌 8 双
  • LINUX nautilus 命令

    最近使用Ubuntu的时候发现了一个很好用的小命令 xff1a nautilus nautilus是GNOME桌面下的一个文件管理工具 通过这个命令我们可以在终端下非常方便的打开指定目录的文件 nautilus 命令后面一个 xff0c 表
  • 无人机学习之路——电机

    一 什么是电机 xff1f 电机是整个无人机的四肢 xff0c 没有电机来驱动的话整个无人机就无法飞起来 因此我们来重点介绍下电机的一些基础知识 本篇内容不会很详细 xff0c 不会全部介绍 xff0c 只会介绍无人机需要学习的知识 二 电
  • c++中“::”和“:”啥意思

    c 43 43 中 和 啥意思 1 1 类作用域操作符 指明了成员函数所属的类 如 xff1a M f s 就表示f xff08 s xff09 是类M的成员函数 2 表示 域操作符 例 xff1a 声明了一个类A xff0c 类A里声明了
  • python中执行shell脚本之subprocess模块

    一 最近subprocess使用背景和介绍 因为最近领导要求 xff0c 在Python端调用大数据的shell脚本 xff0c 所以需要用到Python来执行shell脚本 因此需要查看下subprocess模块文档 根据官网文档描述 x

随机推荐

  • odroid-xu4(ubuntu mate 16.04)源码编译opencv 3.2

    硬件平台 xff1a odroid xu4 软件环境 xff1a ubuntu mate 16 04 1 更新软件列表 xff1a sudo apt get update 2 安装依赖包 xff1a apt get install libq
  • Pytorch Tensor基本数学运算

    1 加法运算 示例代码 xff1a import torch 这两个Tensor加减乘除会对b自动进行Broadcasting a 61 torch rand 3 4 b 61 torch rand 4 c1 61 a 43 b c2 61
  • HC-SR04超声波测距模块介绍

    超声波简介 超声波是由机械振动产生的 可在不同介质中以不同的速度传播 具有定向性好 能量集中 传输过程中衰减较小 反射能力较强等优点 超声波传感器可广泛应用于非接触式检测方法 它不受光线 被测物颜色等影响 对恶劣的工作环境具有一定的适应能力
  • 操作系统之大端小端

    1 什么是大端 什么是小端 所谓的大端模式 xff0c 是指数据的低位保存在内存的高地址中 xff0c 而数据的高位 xff0c 保存在内存的低地址中 xff1b 所谓的小端模式 xff0c 是指数据的低位保存在内存的低地址中 xff0c
  • 激光雷达授时

    文章目录 前言部分原理图调试微信公众号 前言 给Velodyne的16和32线激光雷达授时 用的是 中科微电子 的 ATGM336H 5N31 部分原理图 部分原理图如下 J9是FAKRA接口 直接挂一个GPS有源天线 出来的TX接一个RS
  • TX2/Xavier Linux GPIO 计算

    目录 Linux GPIO子系统TX2 Linux GPIO计算Xavier Linux GPIO计算参考用C快捷计算TX2 Linux GPIO用C快捷计算Xavier Linux GPIODebug微信公众号 Linux GPIO子系统
  • Android CAN 简记

    文章目录 概要adb 连接adb 常用操作CAN配置脚本SocketCANJNIJNI与SocketCANMakefilejnican java生成的 jnican hjnican c运行github jni can NDKAndroid
  • 在C或C++中如何使用PI(π)值

    span class token macro property span class token directive keyword include span span class token string lt math h gt spa
  • 开发一个Nginx模块hello

    开发一个Nginx模块 用C语言写一个ngx http hello module c的文件 C代码 ngx http hello module c Created on Apr 25 2015 Author lizhenbin includ
  • 升级 GCC 支持C++11

    一 错误发生情景 xff1a 使用sh setup sh安装软件时 xff0c 报以下错误 xff1a configure error A compiler with support for C 43 43 11 language feat
  • C++ 访问http接口

    添加头文件 include lt wininet h gt 附加库 xff1a pragma comment lib 34 Wininet lib 34 发送 int GetURLInternal LPCSTR lpszUrl std st
  • go module go.mod

    这一次 xff0c 彻底掌握go mod 1 版本号规范 go mod 对版本号的定义是有一定要求的 xff0c 它要求的格式为 v lt major gt lt minor gt lt patch gt xff0c 如果 major 版本
  • protobuf数据类型

    四 限定符 required optional repeated 的基本规则 1 在每个消息中必须至少留有一个required类型的字段 2 每个消息中可以包含0个或多个optional类型的字段 3 repeated表示的字段可以包含0个
  • gdb如何保存和读取断点

    刚开始在linux下学编程使用gdb的同学可能会发现 xff0c 每次用gdb设置断点调试程序 xff0c 但下次打开的时候所有断点都没有了 xff0c 很不方便 下面介绍保存和读取断点的方法 1 保存断点 先用info b 查看一下目前设
  • C可变参数函数 实现

    C函数要在程序中用到以下这些宏 void va start va list arg ptr prev param type va arg va list arg ptr type void va end va list arg ptr va
  • 灰度发布系统的实现

    灰度发布 xff0c 已经不是一个很新的概念了 xff0e 一个产品 xff0c 如果需要快速迭代开发上线 xff0c 又要保证质量 xff0c 保证刚上线的系统 xff0c 一旦出现问题那么可以很快的控制影响面 xff0c 就需要设计一套
  • 软件汉化教程

    看到网上经常有人问汉化方面的东西 xff0c 我今天也来灌水一篇 xff0c 来个汉化扫盲教程 写的不好的地方欢迎大家指正 xff01 OK xff0c 现在我们进入正题 我这里所说的汉化 xff0c 是指汉化 Windows 下的 PE
  • Python操作mysql数据库(封装基本的增删改查)

    新学Python xff0c 在这里分享操作MySQL的全过程 1 安装MySQL python 1 2 3 win amd64 py2 7 exe xff0c 这是操作mysql数据库的python库 xff0c 有32位和64位之分 x
  • iOS内存管理

    1 内总管理原则 引用计数 IOS的对象都继承于NSObject 该对象有一个方法 retainCount xff0c 内存引用计数 引用计数在很多技术都用到 window下的COM组件 xff0c 多线程的信号量 xff0c 读写锁 xf
  • python 网络框架twisted基础学习及详细讲解

    twisted网络框架的三个基础模块 xff1a Protocol ProtocolFactory Transport 这三个模块是构成twisted服务器端与客户端程序的基本 Protocol xff1a Protocol对象实现协议内容