Building a basic Twisted server

At the time of making a server using the Python socket libraries, a loop was implemented that is in charge of verifying the new connections. We will manage the event handlers with Twisted.

We can manage events for many situations, such as a new connection by a client, the reception of data, or whether a client has been disconnected. These event handlers are defined in a protocol, and this protocol needs a Factory that can build the objects of the events. This may sound confusing, but the code will make everything clearer.

In the following example, we are going to write a basic server using the Twisted framework. You can find the following code in the twisted_basic_server.py file:

#!/usr/bin/python3

from twisted.internet import reactor
from twisted.internet.protocol import Protocol, Factory

class MessageLogger(Protocol):
def connectionMade(self):
print('Client connection from:', self.transport.client)

def connectionLost(self, reason):
print('Client disconnected from:', self.transport.client)

def dataReceived(self, data):
self.transport.write(data)
print("Message sent by the client: ", data.decode("utf-8"))

In the previous code block, we defined our MessageLogger class, which functions as a protocol. In the following code block, we are defining the MessageFactory class for managing the connection. Finally, our main program connects the protocol to a server running on port 8080 using the MessageFactory class:

class MessageFactory(Factory):
def buildProtocol(self, addr):
return MessageLogger()

def clientConnectionFailed(self, connector, reason):
print ("Connection failed")
reactor.stop()

def clientConnectionLost(self, connector, reason):
print ("Connection lost")
reactor.stop()

#this connects the protocol to a server running on port 8080
if __name__ == '__main__':
#factory = Factory()
#factory.protocol = MessageLogger
reactor.listenTCP(8080, MessageFactory())
reactor.run()

We will start by creating a server that forwards everything it receives. Then, we will use a basic client using the standard socket module to test the code.

The first thing we need to do is import the necessary libraries and components, which in this case, are the reactor, protocol, and factory. Then, we will handle the events within a class, such as when we have a new connection, connectionMade, a lost connection, connectionLost, and if we receive data, dataReceived.

This is a simple server program that forwards everything it receives. To achieve this, a protocol must be established. It is for that reason that a new class is created, MessageLogger, of which there will be one instance per connection. The dataReceived method is an event that will be called for each portion of data that has been received. This data is passed to the event in a data argument, which is then used to send what has been received to the client:

class MessageLogger(Protocol):
def dataReceived(self, data):
self.transport.write(data)
print("Message sent by the client: ", data.decode("utf-8"))

self.transport is an instance of twisted.internet.tcp.Server, through which we send data to the client.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset