Basic learning and detailed explanation of python Network Framework twisted

Posted by fcaserio on Tue, 04 Jun 2019 20:32:20 +0200

Three basic modules of twisted network framework: Protocol, ProtocolFactory, Transport. These three modules are the basic components of twisted server-side and client-side programs.

Protocol: Protocol Object Implements Protocol Content, i.e. Communication Content Protocol
ProtocolFactory: A representation of the factory pattern, where protocols are generated
Transport: It is used to send and receive data. The data receiving and processing of server and client are based on this module.

To install twisted in windows, you need to install pywin32 first and download it yourself. Then pip install twisted will help us install twisted and zope.

We combine a graph with a program to understand the basic implementation of twisted:

Then let's first look at the server-side program:

# 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(
            "Welcome to Spread Site, You are number one.%s Twenty Client Users!\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()

Create an endpoint for the IPv4 version of TCP, and then listen. Here we pass in the protocol factory object as a parameter. First we look at our custom factory class SpreadFactory, which is derived from Factory. Then we look at the source code of this class (you need a dictionary:):

@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}
        """
        p = self.protocol()
        p.factory = self
        return p

A very important function here is build Protocol, which creates protocols in factory mode. We implement this function based on the base class Factory. Now let's look at the protocol class Spread derived from Protocol, Spread's Init_ parameter. We pass it a custom SpreadFactory, and then we look at the source code of the base class Protocols.

@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 is also derived from BaseProtocol, continue to look at the source code of this class:

@_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()

As you can see, our custom Spread is just a function that implements the base class. Next we roll aside to implement the logic:
First, we define a list of clients to store connections between multiple clients. When the server receives the connection from the client, we call the connection Made function. At the same time, we send a message to the client using Transport to inform the client that we have received the connection. When the client connection is lost, we call ConnectionLost and remove the client connection from the list. The dataReceived function accepts the data. When the client sends the "close" command, we actively close the connection. Otherwise, we output the data.

Look at the client code:

# 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

At first we set up a TCP connection, passing in the host address, port, and protocol factory object as parameters, and then reactor.run hangs up and runs.
Let's look at the ClientFactory base class, because our custom protocol factory, EchoClientFactory, derives from it. Source code:

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}
        """

Similarly, our custom EchoClientFactory just implements functions that are not implemented in the base class, the most important of which is build Protocol, which generates a protocol for us. Look at our custom protocol class Echo below. The source code of the base class is the same as above.
The client-side protocol function is the same as the server-side protocol function, so let's not say much here.

After the client's twisted module is finished, we create a thread to communicate with the server and send it regularly. Of course, here we need to decide if we have made a connection with the server before sending the message.

About the next basic part, all the code is from "python Effective Development Practice" in the code, here also to recommend this book to you, learning twisted there are two good tutorials, in the end we will share Baidu. The reason for writing this article is to understand the last project of effective development: developing cross-platform Internet of Things message gateway with Twisted. Because the first internship contacted the Internet of Things communication, in the work, I rolled over the source code of the project (written in java, but I also learned C net for two years after all. There is no pressure to understand the source code of the project, mvc orm is similar to EF and MVC in. net, but the grammar is a little different), just like the project in the book, the communication between server and client in the book. The protocol instructions are clear. So it's a good book that we can't miss. It's also the best way to learn and master twisted.

Finally, run the test:

Server side:

Client:

Twitted tutorial: http://pan.baidu.com/s/1dEBPGhN

Topics: Python Attribute Database network