Executing the client and server

First, we need to run the server with the following command:

$ python twisted_basic_server.py

At this moment, the server is waiting for connections from the client. If we run the client, we can write any message in the console and you will see how the server responds with what it has received. The following could be the messages that the server receives from two connected clients:

Client connection from: ('127.0.0.1', 8229)
Message sent by the client: hi this is my message
Message sent by the client: Message from client 1
Client connection from: ('127.0.0.1', 8282)
Message sent by the client: Message from client 2
Client disconnected from: ('127.0.0.1', 8282)
Client disconnected from: ('127.0.0.1', 8229)

The twisted_basic_server.py script starts a TCP server listening for connections on port 8080.

This script sends the information through the transport channel using the MessageLoggerprotocol class. The client socket establishes a TCP connection to the server, resending the server response, terminating the connection, and stopping the reactor. The MessageFactory class is used to connect both client and server, creating instances of the MessageLogger class.

Communication is asynchronous on both sides; connectTCP is in charge of registering the callbacks in the reactor so that we're notified when the information is available to be read from the socket.

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

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