Python Control Server – Multiple Clients (5 of 8)
[ Part 1 – Part 2 – Part 3 – Part 4 – Part 5 – Part 6 – Part 7 – Part 8 ]
We’re off to part five of our little Python control server.
I had been postponing this, but its time we implement multiple clients!
I knew we had to do this right and that it would most likely bloat the scripts as well, so it would also require breaking it down into specific modules.
So yeah, that’s what we’re doing, lets get into it!
But before that, let me give you guys some context into the code…
Modular Programming
Many times when working on a project, as it begins to expand and get into a few hundred lines of code we need to start breaking it up into modules.
For our project, so far, it seems most of the functionality is divided between server configuration and data encryption.
I figure, just for the sake of development and teaching proper methods to keep working in a program without feeling like its a labyrinth, we’ll go ahead and separate our code into a few modules.
Another benefit of this is being able to use the same modules in both the server and client script without any modifications. This is the case for the encryption functions we’ve made in parts three and four.
Text User Interface
Since we’ll be implementing multiple clients in this part, we’re also gonna need a text user interface to switch between these clients.
In the spirit of modular programming I think it makes sense to create a interface module of its own to take care of all that stuff.
This module will only be used by the server to display a simple screen showing how many clients are connected, their IP addresses and so on.
I’ve also added a status screen to display how many clients are connected, their respective IP address and port along with more information.
Threading
Now in order to handle multiple clients, we’re gonna have to use threads.
This is so we can preserve the connections and keep each individual client alive while communicating with different clients at a time.
We’ll be allocating a different port every time a new client connects, so we have to check for available ports prior to spawning a new server thread.
Another key detail is that the main server is now running inside of a thread as well, since we’re using it simply to receive the initial connection and then communicate the newly allocated port to the client.
Download Code
I will update more information about the code and its inner workings once I get some time off work, for now download and check it out. 😉
hi felipe i need some help with task related to sockets with 1 server vs 5 clients which gonna work on rpi’s i will send you full details but first i want to know if i can use ypur help it shuold bw very easy to you because what im looking for its easy but im not so familiar with sockets
thnx man
Hello there!
I am glad to help if possible – send email to: codeonbyte@gmail.com
I’m still working right now so I don’t have too much free time but I’ll be sure to check it out.
Stay safe
We Need To Go Deeper 🙂 Waiting for the next parts…