Simulating a connection channel

There are multiple steps to running the following server example. First, we need to run the script to start the server, from which you will obtain the following output:

> python example1.py
Serving on ('127.0.0.1', 8888)

Notice that the program will run until you invoke the Ctrl + C key combination. With the program still running in one Terminal (this is our server Terminal), open another Terminal and connect to the server (127.0.0.1) at the specified port (8888); this will server as our client Terminal:

telnet 127.0.0.1 8888

Now, you will see some changes in both the server and the client Terminals. Most likely, your client Terminal will have the following output:

> telnet 127.0.0.1 8888
Trying 127.0.0.1...
Connected to localhost.

This is from the interface of the Telnet program, which indicates that we have successfully connected to our local server. The more interesting output is on our server Terminal, and it will be similar to the following:

> python example1.py
Serving on ('127.0.0.1', 8888)
Connection from ('127.0.0.1', 60332)

Recall that this is an information message that we implemented in our EchoServerClientProtocol class—specifically in the connection_made() method. Again, as a connection between the server and a new client is made, this method will be called automatically, in order to initiate the communication. From the output message, we know that the client is making their requests from port 60332 of server 127.0.0.1 (which is the same as the running server, since they are both local).

Another feature that we implemented in the EchoServerClientProtocol class was in the data_received() method. Specifically, we print the decoded data that is sent from the client. To simulate this type of communication, simply type a message in your client Terminal and press the Return (Enter, for Windows) key. You will not see any changes in the client Terminal output, but the server Terminal should print out a message, as specified in the data_received() method of our protocol class.

For example, the following is my server Terminal output when I send the message Hello, World! from my client Terminal:

> python example1.py
Serving on ('127.0.0.1', 8888)
Connection from ('127.0.0.1', 60332)
Data received: 'Hello, World! '

The and characters are simply the return characters included in the message string. With our current protocol, you can send multiple messages to the server, and can even have multiple clients send messages to the server. To implement this, simply open another Terminal and connect to the local server again. You will see from your server Terminal that a different client (from a different port) has made a connection to the server, while the original communication of our server with the old client is still being maintained. This is another result achieved from asynchronous programming, allowing multiple clients to communicate with the same server seamlessly, without using threading or multiprocessing.

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

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