Over the last couple posts we have been putting in work on our server and testing it using netcat. This week, we will be starting work on our client which we can expand on to give us more control over the user experience.
As always, all additional links this project’s github, references, and other sources can be found at the bottom of this post in the “Sauce, References, & Documentation” section.
Advertisements
Starting our Client
Our client should be capable of two very important functions:
receiving messages from server
sending messages to server
We will be planning to perform the above two tasks asynchronously similar to how our Server is setup using asyncio.
Advertisements
Code: ClientModel.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There are no changes to this block of code, but I wanted to take a second to highlight that in this post I have changed the name of what we previously referred to as Client.py to ClientModel.py.
Advertisements
Code: Server.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Line 6: Changed line to import from the new file name “ClientModel”
Line 123: Renamed function to be more descriptive
Advertisements
Code: Client.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Line 7 __init__(): Our client will be initialized with the Server’s IP and Port. Similar to our Server.py code, we will also pass in an event loop to help us manage all of our tasks.
Lines 14-32: Defining property getters for all of our private variables.
Line 34connect_to_server(): Using asyncio, we are going to open a connection to our server which will return to our client a StreamReader and StreamWriter for communication. After we have successfully established a connection to the server, we will then use asyncio to run our two main asynchronous functions: receive_messages() and start_client_cli().
Lines 54-72 receive_messages(): This asynchronous function will be in charge of monitoring our StreamReader. If any messages occurs, it will call our helper function get_server_message() to get the message in utf8 string format. Any received messages will then simply be printed to the user’s terminal.
Lines 74-86start_client_cli(): We will likely plan to expand on this later, but for now this command line interface will enable our users to asynchronously write input to the command line. All messages received by the user will be sent to our server using our StreamWriter.
Advertisements
Testing Our Code
To test our code locally, we will execute the following:
Server python3 Server.py 127.0.0.1 44444
Client 1 python3 Client.py 127.0.0.1 44444
Client 2 python3 Client.py 127.0.0.1 44444
Notice how, for now, we are using a “quit” command to handle server/client disconnects.
Advertisements
Conclusion
In this post, we spent a little bit of time putting together our own Client so that we won’t have to rely on using netcat to connect to our server. There’s a lot more room for improvements, but we at least got the basics of our project setup in a way that we can now perform basic communication.
To be honest, I am not 100% sure where I’d like to take this project from here. I think we now have a pretty solid foundation for anyone to take this work and run with it. If I were to continue this project, I’d probably start looking into developing a more user friendly interface so that anyone could pick up this code and start chatting. Or maybe I’ll look into turning it into a mobile app. Only time can really tell what I’ll end up doing with this project, but I hope you’ve enjoyed what’s here so far!
As always, if you liked what you read and you’d like to support me, please consider buying me some coffee! Every cup of coffee sent my way helps me stay awake after my full-time job so that I can produce more high quality blog posts! Any and all support would be greatly appreciated!
The corgo in glasses is cute 🙂
LikeLike