Python Chat Server Overview (1 of 7)
[ Part 1 (Overview) – Part 2 – Part 3 – Part 4 – Part 5 – Part 6 – Part 7 ]
Welcome to my blog’s first tutorial series!
In this project we will program a Python chat server using threads to make sure we can receive any number of connections while still being able to handle many clients as well – using a single script as the final product for simplicity.
While searching the web, I was unable to find many tutorials that worked like this, so grab some coffee and get ready to start writing some code ayyy!
Fire up your favorite text editor!
Wait, hold up, not so fast there…
Before getting our hands dirty, let’s take a look at some details about this project, for example, what software we’ll need, what will you learn along the way, how exactly is this program going to operate, and so on.
First of all, you will need Python 2.7 (as of right now, the current version is actually 2.7.17). Go ahead and install it from their official page. Note that older versions should work also, but you will be responsible for minor syntax changes.
Second thing you’ll need, of course, is a solid text editor. I highly recommend Sublime Text for many reasons which I could devote a whole post to (and I just might in the future!). In any case, Notepad++ is a solid choice too.
Alright, that’s all the software we need. On to the project…
What you will learn
The main goal of this project is to teach different components, such as:
- Using timestamps
- User input and output
- Text-based user interface (TUI)
- Handling multiple threads for client/server
- Understanding socket connections in networking
- Keeping track of logs for the system and conversations
- …among others that I’m lazy to think of right this moment 😉
The final program will be a single file instead of the usual client-server dynamic. The idea is to have a single script the user can run to perform both functions. That’s not to say that we won’t initially develop it separately, because we will. Around half-way through the project – part 5 to be precise – we will consolidate the files to perform both functions (since this is the desired behavior after all).
If you would like to understand how the final program is going to operate, keep reading below, otherwise simply jump to the next part of the series.
- The program will initially do basic user input – ask for the username.
- Then we’ll display the main text-based user interface to the user.
- After that we will run a main server and listen for a single client.
- Once a client connects, we allocate a port and spawn a server thread.
- Now we communicate this port to the user connected on the main server – after this the main server is released and starts listening again.
- At this point, the user will connect to the new thread and communicate his newly allocated port back to us.
- Then we’ll spawn a client thread and connect to this port.
- Finally send a message to confirm the connection is established.
Yeah, yeah, yeah… I know… it sounds a little complicated.
But that’s exactly why we’re breaking it up in 7 parts, so it is easy to digest for you: the readers/viewers! To make it even easier, I decided to do the first few parts in the traditional client-server model with 2 files while making very slow progress along the way, ensuring we dive into many topics.
After all, the journey matters more than the destination; and, of course, that sweet knowledge you’ll get to keep once it’s all said and done.
Leave a Comment